Imagine, your company is newly established, you are an IT Manager, your task is to create 1000 Windows users for 1000 employees. If you are a regular user, you will go to Control Panel, click on User and create an account? Is not! If it is the IT Manager, they will write scripts on Powershell to automate User creation.
On Windows, PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change system settings and automate tasks. It is similar to Command Prompt, since Microsoft originally created PowerShell to replace Command Prompt. However, PowerShell has more capabilities than a command-line interface (CLI) such as providing an extensive toolkit, and has more flexible control capabilities. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.
A script is just a set of lines of code saved to a text file (using the file extension “.ps1”) that PowerShell can understand and execute to perform various actions.
A little bit confusing is that the default security protocol always blocks any scripts from running on the device. This means that when double clicking on the file “.ps1” on Windows 10 nothing will happen and if you try to run the script in PowerShell, you will see the error “cannot be loaded because running scripts is disabled on this system” . However, it is not impossible to run the script on your computer. You just need to enable a correct executable number,
So, in this article, I will guide you through the steps to successfully write and run your first script on PowerShell using Visual Studio Code, Notepad and the PowerShell Integrated Scripting Environment (ISE) control panel.
How to create a PowerShell script on Windows 10
On Windows 10, you can create a PowerShell script file using virtually any text editor or ISE console. However, I recommend using Visual Studio Code (my hobby :v) to write scripts and use the PowerShell extension.
Create scripts using Visual Studio Code
Visual Studio Code – also known as VS Code – is a free cross-platform code editor and can use extensions that provide an environment for writing virtually all programming languages. And when you add the PowerShell extension, you get a completely different scripting experience, even with the help of IntelliSense (self-completion of code).
The new experience doesn’t mean it will be default, but the PowerShell ISE won’t disappear either. However, ISE will not add new features and will not support PowerShell versions 7 and above.
Install Visual Studio Code
To install Visual Studio Code on Windows 10, you need to follow these steps:
1. Go to the Download page of Visual Studio.
2. Click the Windows button to load the installer.
3. Double click on the downloaded file to start installing VS Code.
4. Agree to the terms of the agreement.
5. Click Next.
6. Continue to click Next.
7. Click Next again.
8. Select some additional necessary additional options (it’s your own choice).
9. Click Next.
10. Click the Install button.
11. Finally click Finish.
After completing the steps above, you need to continue to install the PowerShell extension.
Install the PowerShell extension
To install the PowerShell extension on VS Code, you need to do the following:
1. Open VS Code.
2. Click the Extension tab on the left panel.
3. Search for PowerShell and select the first result.
4. Click the Install button.
So you can start writing Script already.
Create PowerShell script on Visual Studio Code
1. Open VS Code.
2. Click File on the menu and select New File.
3. Select File and click Save as.
4. In the “File name” section you must name the file with the extension .ps1, for example: first_script.ps1.
5. Click the Save button.
6. Paste the following command into your script and run:
Write-Host "Congratulations! Your first script executed successfully"
The above command will output the phrase “Congratulations! Your first script executed successfully ”on the screen.
7. Press the Run button on the top right or press F5 to run the script.
8. Click File on the menu.
9. Click Save.
Create PowerShell Script in Notepad
1. Press Start Menu.
2. Search for Notepad and select the first result.
3. Paste the following command into Notepad:
Write-Host "Congratulations! Your first script executed successfully"
4. Click File.
5. Select Save as.
6. Write the filename with the .ps1 extension, for example: first_script.ps1.
7. Finally click Save.
Script creation using the Integrated Scripting Environment
Alternatively, you can use the built-in PowerShell ISE control panel to write scripts on Windows 10.
The Integrated Scripting Environment is an advanced PowerShell development tool, but you can get started with these steps:
1. Press Start Menu.
2. Search Windows PowerShell ISE, right-click the first result and select Run as administrator.
3. Click File.
4. Select New to create the .ps1 file.
5. Paste the code below into:
Write-Host "Congratulations! Your first script executed successfully"
6. Click File.
7. Select Save.
8. Enter the script name, for example: first_script.ps1.
9. Select where to save the script.
10. Finally click Save.
After you complete the above steps using Notepad, Visual Studio Code, or PowerShell ISE, the script is ready to run, but it will crash by default. This is because PowerShell is installed by default to always block the execution of any scripts. (The only exception is if you run the script in Visual Studio Code or PowerShell ISE.)
How to run a PowerShell script on Windows 10
If you want to run scripts using PowerShell, you have to change the execution policies on Windows 10.
To change the execution and run policy for PowerShell scripts, you need to do the following:
1. Press Start Menu.
2. Search for PowerShell, right-click the first result, and select Run as administrator.
3. Paste the command below into PowerShell and press Enter:
Set-ExecutionPolicy RemoteSigned
4. Type A and press Enter (if required by PowerShell).
5. Type the following command to run the script and press Enter:
& "C:\PATH\TO\SCRIPT\first_script.ps1"
In the above command, make sure to change “PATH\TO\SCRIPT” to the location of the script on your machine.
For example, this command runs mscript stored in the Download folder:
& "C:\Users\username\Downloads\first_script.ps1"
After you have completed the above steps, you should be able to run the script.
On Windows 10, PowerShell includes four execution policies:
- Restricted – Stop running any scripts.
- RemoteSigned – Allows scripts to be created on the device, but scripts created on other computers won’t run unless they are signed by trusted developers.
- AllSigned – All scripts will be run, but only scripts with a trusted signature from their developer.
- Unrestricted – Run any script without any restrictions.
In the above article, I used the command to allow local scripts to run on Windows 10. However, if you don’t plan to run this script often, you can restore default setting to block the scripts. Unreliable command using the Set-ExecutionPolicy Restricted
command.
Download PowerShell textbooks and exercises
TechMody has compiled 2 of the most complete Ebooks about PowerShell for you to self-study. The content of the ebook is easy to understand, with more than 100 exercises for you to practice on your own. If you are a network administrator or you are going deeper into PowerShell programming, then you should check out this ebook
Download Ebook and PowerShell exercises
The content includes:
- PowerShell Notes for Professionals book
- Windows Powershell 3.0 Step by Step
- 100 Powershell exercises and mockups