Introduction
Windows PowerShell is a command-line interface and programming language designed specifically for the management and automation of Windows operating systems and related Microsoft products. It was first released in 2006 and has played a significant role in the system administration of Windows systems ever since.
A PowerShell script consists of a sequence of PowerShell cmdlets, functions, variables, conditions, and other instructions to automate repetitive processes and tasks. PowerShell scripts are stored in text files with the .ps1 file extension. You can use any text editor or the PowerShell Integrated Scripting Environment (ISE) console to create them. To run a script, open the PowerShell console and enter the path to the script file.
Regardless of how you invoke a PowerShell script, you need to ensure that the script execution policies are configured correctly in your PowerShell environment. By default, PowerShell script execution is disabled on many systems to minimize security risks. You can change the execution policies using the Set-ExecutionPolicy cmdlet.
How to create a PowerShell script using Visual Studio Code
Visual Studio Code (VS Code) is a popular and free code editor from Microsoft that is specifically designed for creating scripts and applications. It includes powerful syntax highlighting for PowerShell code and supports IntelliSense, which allows for automatic code completion.
Step 1: Install VS Code
If you don't have VS Code installed, you can download it from the official website. Select Windows as the operating system and double-click to start the installation.
Step 2: Add the PowerShell plugin
To be able to use PowerShell effectively in VS Code, you need to install the official PowerShell extension from Microsoft. To do this, go to the Extensions section in VS Code (the icon in the left sidebar) and search for PowerShell. Or use the shortcut [Ctrl] + [Shift] + [X]. Click Trust Workspace & Install to install the extension.
Step 3: Create the PowerShell script
You can open an existing PowerShell script or create a new script by selecting File > New Text File and then entering a file name including the “.ps1” extension under Save As.
Here is an example of PowerShell code that can be written to the open file:
$message = "Script to be executed" Write-Host $messageStep 4: Run the PowerShell script
VS Code includes a built-in terminal that you can use to run PowerShell commands directly in the environment. Open the terminal by clicking Terminal > New Terminal and selecting PowerShell as the terminal type.
How to create a PowerShell script using Notepad
In this section, we will show you how to create a PowerShell script using the Notepad text editor.
Step 1: Open Notepad
Click on Start or on the Windows icon in the lower left corner of your desktop. Type “Notepad” in the search bar and press Enter. This will open the Notepad text editor.
Step 2: Write the PowerShell code
You can paste PowerShell code directly into the Notepad editor.
Click File > Save or use the key combination [Ctrl] + [S]. Enter a file name and add “ps1.” to the end to save the script as a PowerShell file. Choose a storage location on your computer and click Save.
How to create a PowerShell script using the Integrated Scripting Environment (ISE)
PowerShell Integrated Scripting Environment (ISE) is an integrated development environment (IDE) from Microsoft. PowerShell ISE is included by default on Windows systems and is a powerful and user-friendly environment for developing PowerShell scripts. Note that ISE has been replaced by Visual Studio Code (VS Code) in PowerShell 5.0 and later, as it has additional features and flexibility.
Step 1: Open PowerShell ISE
Click Start in the lower left corner of your desktop. Type “PowerShell ISE” in the search bar and select Run as administrator.
Step 2: Create a new script
In PowerShell ISE, you can create a new script by clicking File > New or by using the key combination [Ctrl] + [N]. Write your PowerShell code in the main ISE window. You have access to features such as syntax highlighting, code autocompletion, and a clear user interface that makes script development easier.
Here is an example of a simple PowerShell script:
# This is a comment $message = "Hello World!" Write-Host $messageStep 3: Save the script
Click File > Save or press [Ctrl] + [S]. To save the script as a PowerShell file, make sure to add “.ps1” to the end.
How to run a PowerShell script
A PowerShell script is usually started through the PowerShell console or another terminal.
Step 1: Launch PowerShell
First, open PowerShell with administrator rights like in the ISE example.
Step 2: Change the execution policy
PowerShell has four different execution policies that control the security and ability to run scripts in the PowerShell environment. The four execution policies are:
- Restricted: This is the default execution policy for PowerShell. With this policy, scripts are disabled and only interactive commands can be executed in the console. Since this prevents all scripts from running, it provides the highest level of security.
- AllSigned: With this policy, all scripts must be digitally signed to run. This means that the script writer must use a digital certificate to sign the script.
- RemoteSigned: With RemoteSigned, only scripts that originate from the Internet or from a network location need to be signed. Local scripts that are stored on your computer can be executed without signing. This makes it easier to use local scripts.
- Unrestricted: This policy allows all scripts to run without signing or restrictions. Using this policy in a production environment is strictly prohibited as it is a security risk. It should only be considered for testing purposes or in secure environments.
You can view the current execution policy in your PowerShell environment using the Get-ExecutionPolicy command. To change the execution policy, use the Set-ExecutionPolicy command followed by the policy you want.
You can enter the following command to allow scripts to run in PowerShell:
Set-ExecutionPolicy RemoteSigned
Step 3: Confirm execution (if necessary)
Depending on the security settings of your PowerShell environment, you may receive a security message to confirm that you want to run the script. Enter "Y" or "A" to agree, or "N" if you do not want to run.
Step 4: Run the Powershell script
To run the PowerShell script, enter the file path:
& "C:\PATH\TO\SCRIPT\script.ps1""















