What are PowerShell Scripts?
PowerShell scripts are lists of commands executed by PowerShell scripting engine. They are text documents with instructions written using PowerShell scripting language. They are used to automate computer processes. All PowerShell scripts are saved as .PS1 extension.
How to run scripts?
Using PowerShell: To run a script which is saved at C:\Temp\Script1.ps1, just type the full path C:\Temp\Script1.ps1 or if in the same directory, .\Script1.ps1
Windows Explorer: Right click the script file and select Run with PowerShell
Execution Policy
To prevent the execution of malicious scripts, PowerShell enforces an execution policy. By default, the execution policy is set to Restricted, which means that PowerShell scripts will not run. You can determine the current execution policy by using the following cmdlet:
Get-ExecutionPolicy
The execution policies you can use are:
- Restricted – Scripts won’t run.
- RemoteSigned – Scripts created locally will run, but those downloaded from the Internet will not (unless they are digitally signed by a trusted publisher).
- AllSigned – Scripts will run only if a trusted publisher has signed them.
- Unrestricted – Scripts will run regardless of where they have come from and whether they are signed.
You can set PowerShell’s execution policy by using the following cmdlet:
Set-ExecutionPolicy <policy name>
Scripting Tools:
PowerShell ISE:
The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In the ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface. The ISE provides multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages. So we can use PowerShell ISE to write and test our scripts.
Visual Studio Code:
Visual Studio Code is a source-code editor made by Microsoft for Windows, Linux and macOS. Features include support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git. This tool is very popular now a days. VS Code can be downloaded from https://code.visualstudio.com/download
You can also use any text editor like notepad, notepad++ etc. to create PowerShell scripts.