Create and Run a PowerShell Script
Create a PowerShell script
On Windows 10, a user can create the PowerShell script using any text editor or the Integrated Scripting Environment (ISE), which is already available on every system.
Create a Script using Notepad
The following steps are used to create a PowerShell script:
1) Open Start.
2) Search for text editor such as Notepad, and click on the Notepad app.
3) Now, a new window of notepad will open. Type the script on the text file which you want to execute. For example:
4) Click on the ‘File‘ menu in the menu bar.
5) Select the option ‘save as‘ from the drop-down list.
6) Type the name of a file in the box and click on the ‘save‘ button. For example: fs.ps1
7) At last, click on a save button.
Create a Script using the Integrated Scripting Environment
The following steps are used to create a PowerShell script using the ISE tool:
1) Open Start.
2) Search for the Windows PowerShell ISE, right-click on the ‘Windows PowerShell ISE‘ and select the ‘Run as Administrator‘ option from the drop-down list.
3) Click on the File in the menu bar.
4) Select the ‘New’ option to create a new empty file.
5) Write a script which you want to execute. For example:
6) Click on the ‘File‘ menu in the menu bar.
7) Select the option ‘save as‘ from the drop-down list.
8) Type the name of a script. And, then click on the Save button.
Enable PowerShell Scripts
When we start the PowerShell in a computer system, the default execution policy does not allow us to execute or run the scripts.
There are four different types of execution policy in PowerShell:
- Restricted: In this policy, no script is executed.
- RemoteSigned: In this policy, only those scripts are run, which are downloaded from the Internet, and these must be signed by the trusted publisher.
- Unrestricted: All the scripts of Windows PowerShell are run.
- AllSigned: Only those scripts can be run, which are signed by a trusted publisher.
Because the default execution policy of Windows PowerShell is restricted, so we cannot run any script until we change it. First, we set the execution policy to Unrestricted to execute the scripts by using the following command.
Run a PowerShell script
To execute the PowerShell script from the command line, follow the given steps:
1) Type the Windows PowerShell in the start menu and then open it by clicking on a result.
2) Now, execute the script by typing the full path to the script such as (C:/desktop/fs.ps1), or if it is in current directory, type the file name followed by a backslash.