How to create and run a PowerShell script file on Windows 10
A script is a collection of commands stored in a text file (using the special extension .ps1) that PowerShell understands and executes step by step to trigger a variety of operations.
In this article, TipsMake will guide you how to create and run a PowerShell script file on Windows 10.
PowerShell is a command line tool designed by Microsoft to run commands and scripts to change settings or automate tasks. It sounds like Command Prompt. However, PowerShell is a more functional command line interface (CLI) with a variety of toolkits and flexible control and control capabilities.
While Command Prompt is only available on Windows, PowerShell is available on Windows, macOS, and Linux.
To create and run a PowerShell script file on Windows 10, follow these steps:
How to create a PowerShell script file
On Windows 10, you can virtually create a PowerShell script file using any text editor or ISE (Integrated Scripting Environment). However, it's best to use the Visual Sturio Code editor with the PowerShell extension.
Visual Studio Code - also known as VS Code - is a free and extensible cross-platform code editor. With VS Code, we can compose and edit almost any kind of programming language. And when you add the PowerShell extension, you'll have a fully interactive scripting experience.
So, the first thing you need to do is download and install VS Code:
- Visit Microsoft's VS Code download page here.
- Click the Windows button to download the installer
- Double click the left mouse button on the downloaded file to run the VS Code installer
- Agree to the terms and conditions
- Click Next
- Click Next again
- Click Next for the third time
- Confirm additional actions
- Click Next
- Click Install
- Wait for the loader to finish and click Finish
After downloading and installing VS Code, you need to install the PowerShell extension. Here's how to do it:
- Open VS Code
- Click the Extensions tab on the left side or click Ctrl + Shift + X
- Search for PowerShell and click on the first result
- Click the Install button
When you're done, you can start writing the PowerShell script file using VS Code:
- Open VS Code
- Click the File menu and choose New File
- Click the File menu and choose Save as
- In the File name field , name the file with the .ps1 extension , for example make tech easier.
- Click Save
You can write or paste the script you want to run, for example:
Write-Host "Welcome tipsmake script"
The script above when running will display the following text below the screen:
Welcome tipsmake script
You can run the script by pressing the Run button on the upper right side or pressing the F5 shortcut . After writing the script you should press the File menu and choose Save to save.
After you have finished, your script should be able to run but not execute your commands. This is because by default PowerShell is always set to block any script action. The only exception is when you run the script in VS Code or PowerShell ISE.
How to run a PowerShell script file on Windows 10
To run a script file with PowerShell you need to change the execution policy on Windows 10.
On Windows 10, PowerShell includes 4 enforcement policies including:
- Restricted - Do not allow any scripts to run
- RemoteSigned - Allows you to run scripts created on the device but block scripts created on other computers unless they are signed by a trusted entity.
- AllSigned - All scripts will run when they are signed by a trusted entity
- Unrestricted - Run any script on the computer, without any restrictions
Here's how you set up to run PowerShell scripts on Windows 10:
- Press Windows to open the Start menu
- Type
powershell
then right-click on the search results to run PowerShell as Administrator - In the PowerShell window , enter the following command and then press Enter :
Set-ExecutionPolicy RemoteSigned
- Type A and press Enter
- Next, you use the following command to run the script. Remember to change the path and filename of the script file to suit your needs:
& "C:/PATHTOSCRIPT/tipsmake_script.ps1"
Above is how to create and run PowerShell script files on Windows 10. Wish you success and invite you to read more great tips on our site.
You should read it
- How to convert PowerShell script file (.ps1) to .exe by IExpress on Windows 10
- How to Enable and Use Script Execution Policy in Windows PowerShell
- How to install PowerShell 7.0 in Windows 10/8/7
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 5
- How are Command Prompt (cmd) and PowerShell different?
- 10 PowerShell commands help you manage the remote computer most effectively
- About PowerShell
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 3
- Test SQL Server with Windows PowerShell - Part 1
- Use PowerShell to download any file on Windows 10
- 10 tips with PowerShell in Windows Server 2008 - Part 1
- Test SQL Server with Windows PowerShell - Part 2