Share this article
Improve this guide
How to create PowerShell scripts on Windows 10 and Windows 11 with Visual Studio Code
5 min. read
Published onFebruary 9, 2022
published onFebruary 9, 2022
Share this article
Improve this guide
Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more
In this article
Toggle
Did you know that you can create PowerShell scripts on Windows 10 and Windows 11? Now that youinstalled 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 10andWindows 11.
First, you need to learn how tocreate a basic PowerShell script. Once you learn that, you will be able to create and run PowerShellscriptson 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 forwardis to use theVisual Studio Codeeditor 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 toprepare your PCto create PowerShell scripts on Windows 10.
Install VS Code
1.Download the VS Code installer on Windows2. Once downloaded, run the installer (VSCodeUserSetup-{version}.exe) to install VS Code
Alternatively, you canalso download a Zip archive, extract it, and run the VS Code installer from there.
Once VS Code is installed, thePowerShell extensionneeds 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
- OpenVS Code2. ClickExtensionsfrom the left pane (arrow indicator)3. Search for and select “PowerShell”4. ClickInstallbutton to install thePowerShell(orPowerShell 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 shortcutCtrl + Shift + Xto 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 + Pkeyboard shortcut.
Create PowerShell scripts with VS Code
Here’s what to do to create a script:
- OpenVS Code2. Click theFilemenu and select theNew Fileoption. Alternatively,use theCtrl + Nkeyboard shortcut3. From here, you can go to File menu again, then selectSave Asto change the file type. Alternatively, use the keyboard shortcutCtrl + Shift + S4. Change the File name and location as you wish on your PC. Change the “Save as type” toPowerShellas shown5. ClickSaveto save the file
Now that a PowerShell script file is created, it should appear in your files as a Windows Powershell file, with a.ps1file 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.
- Open the file you created in VS Code2. 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 theRunin the upper right or press F5 to run the script in an integrated PowerShell window and see the output below as shown4. Click theFilemenu and clickSavewhen 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.
- OpenPowerShellandRun as Administrator2. Copy and paste the following command to run PowerShell scripts:Set-ExecutionPolicy RemoteSigned3. PressEnterto run the command4. TypeAand PressEnter
Now that the Windows 10 execution policy has been changed in PowerShell, it’s time to run the PowerShell script.1. Open PowerShell andRun as Administrator2. Paste the file path to the PowerShell script file that was createdC:\Users\DAVE\Downloads\TestScript.ps13. PressEnterto 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 theOnMSFTresources 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!
davew
User forum
0 messages
Sort by:LatestOldestMost Votes
Comment*
Name*
Email*
Commenting as.Not you?
Save information for future comments
Comment
Δ
davew