Scripts

A script is nothing more than the same or similar commands that you would run interactively in the PowerShell console, except they’re saved as a PS1-file. A script file

Execution Policy of scripts

Before you can run a script on Windows, you need to change the default PowerShell execution policy. The default execution policy, Restricted, prevents all scripts from running, including scripts that you write on the local computer. The execution policy is saved in the registry, so you need to change it only once on each computer.

You can set the execution policy with cmdlet Set-ExecutionPolicy. The policy bypass means that nothing is blocked and there are no warnings or prompts. Please note, that you have run it as Admiministrator. Set Execution Policy Note: that execution policy does not apply to PowerShell running on non-Windows platforms.

Read more about Execution Policy

Run a script

To run a script, type the full name and the full path of the script file.

For example, to run the getAccounts.ps1 script in the C:\Scripts-directory, type:

C:\Scripts\getAccounts.ps1

To run a script in the current directory, type the path to the current directory, or use a dot to represent the current directory, followed by a path backslash (.\).

For example, to run the getAccounts.ps1 script in the local directory, type:

.\getAccounts.ps1

If the script has parameters, type the parameters and parameter values after the script filename. For example, to run the getAccounts.ps1 script with a parameter, type:

.\getAccounts.ps1 Jakke

Parameters in scripts

Script parameters work like function parameters. The parameter values are available to all of the commands in the script. All of the features of function parameters, including the Parameter attribute and its named arguments, are also valid in scripts.

To define parameters in a script, use a Param statement. The Param statement must be the first statement in a script, except for comments and any #Require statements.

In the following example there is a script named params2.ps1. The script takes two parameters.

#script takes two named arguments
#arguments have default values if there are not given
param (
  [string]$computer = '192.168.8.1',
  [int]$tries = 1
  )
for ($i = 0; $i -lt $tries; $i++)
  {Write-Output("try " + ($i +1) + " to ping: " + $computer)} 

We call a script with it's arguments. Arguments are given after the name of the script, seperated with space. We can also call the script without parameters, then it will use default values.

#without parameters
.\params2.ps1
#with parameters
.\params2.ps1 192.168.8.8 3

Two parameters

Editors for Scripting

Scripts can be written with any good editor.

PowerShell ISE

PowerShell comes with a graphical tool named PowerShell ISE. Start it from Windows Start.

start ISE

With PowerShell ISE you can write and test scripts. run ISE

ISE have help to show all commands what you can use, s you can easily find and test PowerShell commands in ISE without saving a script: help in ISE

Visual Studio Code with extension

Visual Studio Code is also an excellent tool for writing and testing PowerShell scripts. Install the extension named PowerShell to your Visual Studio Code and start scripting :-) VS Code Extension