How to create PowerShell scripts on Windows 10 and Windows 11 with Visual Studio Code
5 min. read
Published on
Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more
Did you know that you can create PowerShell scripts on Windows 10 and Windows 11? Now that you installed PowerShell on Windows, you might want to know what you can do with it on your PC. In this guide, we will show you how to create a simple script file using Visual Studio Code and run the script file in PowerShell on Windows 10 and Windows 11.
First, you need to learn how to create a basic PowerShell script. Once you learn that, you will be able to create and run PowerShell scripts on Windows 10 and Windows 11 to make your workflow more efficient thus making you more productive.
How to create PowerShell scripts on Windows 10 and Windows 11
Previously, you could create scripts using almost any text editor. However, Microsoft’s preferred option moving forward is to use the Visual Studio Code editor with the PowerShell extension.
Visual Studio Code (VS Code) is a powerful, but lightweight source code editor that runs on your desktop and is available on Windows, macOS, and Linux.
VS Code comes with built-in support for JavaScript, TypeScript and Node.js. As an added benefit, you get a rich ecosystem of extensions for other development languages, including C++, C#, Java, Python, PHP, Go, as well as and runtimes, such as .NET and Unity.
In this guide, we will concentrate on what to do to prepare your PC to create PowerShell scripts on Windows 10.
Install VS Code
1. Download the VS Code installer on Windows
2. Once downloaded, run the installer (VSCodeUserSetup-{version}.exe) to install VS Code
Alternatively, you can also download a Zip archive, extract it, and run the VS Code installer from there.
Once VS Code is installed, the PowerShell extension needs to be installed to allow for PowerShell scripts to be created.
Install PowerShell extension on VS Code
Here’s how to open VS Code and install the PowerShell extension
1. Open VS Code
2. Click Extensions from the left pane (arrow indicator)
3. Search for and select “PowerShell”
4. Click Install button to install the PowerShell (or PowerShell Preview) extension on VS Code
After you complete the steps, you can start creating PowerShell scripts on Windows 10 with VS Code.
If you want to install other extensions in the future, use the keyboard shortcut Ctrl + Shift + X to be taken directly to the VS Code extensions. If you have the time, you can familiarize with other keyboard shortcuts available by using “Showing All Commands” in VS Code, Ctrl + Shift + P keyboard shortcut.
Create PowerShell scripts with VS Code
Here’s what to do to create a script:
1. Open VS Code
2. Click the File menu and select the New File option. Alternatively,
use the Ctrl + N keyboard shortcut
3. From here, you can go to File menu again, then select Save As to change the file type. Alternatively, use the keyboard shortcut Ctrl + Shift + S
4. Change the File name and location as you wish on your PC. Change the “Save as type” to PowerShell as shown
5. Click Save to save the file
Now that a PowerShell script file is created, it should appear in your files as a Windows Powershell file, with a .ps1 file extension. With the newly created “TestScript” PowerShell file, we can add a command for the file to run in PowerShell. Here’s what to do.
1. Open the file you created in VS Code
2. Write a new or paste a script that you want to run:
Write-Host "Hello world! I created a PowerShell script!"
The script above will output the phrase: “Hello world! I created a PowerShell script!”
3. (Optional) You can use the Run in the upper right or press F5 to run the script in an integrated PowerShell window and see the output below as shown
4. Click the File menu and click Save when you are finished to save the file
Change the Windows execution policy in PowerShell
Once you complete the steps using VS Code, the PowerShell script will be ready to run. But before you run your script file, you need to change the execution policy on Windows 10. By default, scripts are not authorized to run in PowerShell.
Here’s how to change that.
1. Open PowerShell and Run as Administrator
2. Copy and paste the following command to run PowerShell scripts:
Set-ExecutionPolicy RemoteSigned
3. Press Enter to run the command
4. Type A and Press Enter
Now that the Windows 10 execution policy has been changed in PowerShell, it’s time to run the PowerShell script.
1. Open PowerShell and Run as Administrator
2. Paste the file path to the PowerShell script file that was created
C:\Users\DAVE\Downloads\TestScript.ps1
3. Press Enter to run the script
After you complete the steps, the PowerShell script will run. If the script was written correctly, you should see its output in PowerShell.
More Windows 10 and Windows 11 resources
For more helpful articles, coverage, and answers to frequent questions about Windows 10, visit the OnMSFT resources available below.
Windows 10 on OnMSFT – All you need to know
Windows 11 on OnMSFT – All you need to know
OnPodcast: the OnMSFT Podcast on YouTube
Have any suggestions for Windows 10 and Windows 11 tips and tricks? Let us know in the comments!
User forum
0 messages