If you are new to PowerShell you can use this article to create your first PowerShell file that actually does something.
This is article will work for the latest Windows 10 PowerShell.
- What is a PowerShell Script?
- How to create a PowerShell Script file in NotePad?
- How to create a PowerShell Script file using the ISE?
- How to run a PowerShell file in Windows 10?
- What are the different Permissions for Executing the Scripts in Windows 10?
Windows 10 PowerShell is a command-line tool that is used for scripting. We can use PowerShell to change settings, automate tasks and more.
What is a PowerShell Script?
A PowerShell script file is just a set of scripts saved to a file with a file extension ‘.ps1‘. But we will not be able to run a PowerShell script by just double-clicking it. We need to set the correct execution policy for that. Before we get to that, let us see how to create a PowerShell file.
How to Create a PowerShell Script file in NotePad?
We will write a PowerShell script in a notepad file using the instructions given below. We will make a script that will fetch all the services that are stopped. We will do that by the below script.
If we execute that script on PowerShell, we will get the following output.
We will save the script to a file by the following steps.
- Open notepad.
- Paste the script.
- Save the file with a .ps1 file extension as given below.
- Note the .ps1 file extension.
We have successfully created the PowerShell script file.
How to create a PowerShell Script file using ISE?
The ISE stands for Integrated Service Environment. Use the following steps for launching an ISE.
- Press the start button.
- Type PowerShell.
- Choose the option shown in the image.
- You will see a small loading screen as shown here.
- This is PowerShell ISE.
- Enter the command as shown in the image.
- Save the file using ctrl+s.
- Press f5 function key to run the script and see the output.
This is how we create a PowerShell file using the ISE.
How to run a PowerShell file in Windows 10?
We have learned how to create a PowerShell file in Windows NotePad. We have also learned how to create a PowerShell file in the PowerShell ISE. But now we will see how to run a PowerShell file.
- Launch PowerShell.
- Enter the following command.
- You need to select ‘a‘ as it will allow all the scripts to run.
- Enter the file path and the file name and press enter.
This will execute the script.
What are the different permissions for executing PowerShell Script?
We can use the following command to get help regarding the set-execution policy.
It gives us the following output.
Here is a brief explanation of some of the most used policies.
- Unrestricted – It is to run any script without any restrictions.
- AllSigned – This policy will let only the signed scripts to run. The scripts must be signed by a trusted publisher.
- RemoteSigned – If a script is signed on the remote device, it will run on the remote device. However, if the script is signed on the local device, the script must be signed using a trusted publisher.
- Restricted – This will block all the scripts from running from a file.
We learned much about creating a PowerShell file. We entered the scripts in it and saved it. The PowerShell file must be executed after setting the appropriate execution policy.
If you want more posts on PowerShell, stay tuned to TecKangaroo.
- Article Rating
- Content Relevance
- Content Clarity
- Author Rating