PowerShell Scripts — Start with the Basics

Shammi Kolonne
4 min readJan 16, 2021

Windows PowerShell is a cross-platform framework comprising a command-line shell and a scripting language. With PowerShell, you can automate various tasks through commands, also known as “cmdlets”. If you want to execute a set of commands all at once, you could do so through PowerShell scripts. These scripts are executable files that use the .ps1 extension and contains a collection of commands inside them. This article will cover the basics you need to know to get started on writing PowerShell scripts and running them.

Creating a PowerShell script is as simple as can be. You can create the script using a plain text editor or a preferred code editor and save it as a .ps1 file. But before running the script, you have to change PowerShell’s execution policy.

PowerShell Execution Policy

PowerShell execution policy is a feature that ensures safety from malicious scripts by controlling which scripts PowerShell can run. You can get the current execution policy in your computer with the Get-ExecutionPolicy cmdlet. The default execution policy for Windows client computers, which is Restricted, does not allow any scripts to run unless the script is located in the Windows system directory, C:\WINDOWS\system32 . Non-Windows machines have Unrestricted as their default execution policy which has no restrictions when running scripts apart from giving a warning.

In Windows computers, the execution policy can be changed using the below command,

Set-ExecutionPolicy -ExecutionPolicy <PolicyName>

Non-Windows machines do not allow this.

It is also possible to set execution policies to affect a particular scope. A scope controls which users will be affected by the execution policy. The default scope is LocalMachine which affects all users of the computer. The scope can be changed using the cmdlet below,

Set-ExecutionPolicy -ExecutionPolicy <PolicyName> -Scope <scope>

You can learn more about execution policies and scopes by referring the Microsoft documentation.

Now that you have dealt with the execution policy, it’s time to run the script you have created.

Running a PowerShell Script

There are a few ways you could use to run your scripts.

One way is to give the full path along with the script name without navigating to the location.

PS C:\Users\User> <file_path_to_script>\<script name>.ps1

But rather than providing the entire path every time you want to run the script, it is much easier to navigate to the path and then execute the script from there. Once you have navigated to the correct location, you can simply enter ./<script name> to execute it.

PS <file_path_to_script>> ./<script name>.ps1

This script would work even without the extension, ./<script name>

Variables, Functions and Parameter Passing

Just like with any other programming language, you could use variables, write functions and pass parameters to them when writing your scripts.

Let’s take a look at the example function below.

The script will not be able to bind the arguments passed in with the parameters unless they are declared at the beginning of the script. Declaring them again inside the functions is not mandatory.

After Test-Path verifies the provided argument $filepath to be a valid path, get-content will read the file in that location and save its contents to the variable $content. Then the file contents will be printed out on the console. These cmdlets use flags, like –Path used above, to identify the parameters that are being passed into it.

The created functions can be called along with the arguments. The flag –filepath is not compulsory, but can nevertheless be used to specify the arguments that should be passed in when executing the script. Using flags can come in handy when there are multiple arguments.

We can pass in a file path as the argument along with the flag when executing the script.

./script1 -filepath <file path>

Pipelines

Pipelines is a special feature in PowerShell that allows us to group a series of commands together. The commands will be written separated by the pipeline operator| and it will feed the results of the preceding command to the next one.

Get-Service will return a list of the services in your computer and this list will be passed on to the next command Out-File which will write the results to the file located in the provided file path.

Handling Exceptions

There’s no escaping from exceptions even if it’s PowerShell you are using. You can catch exceptions just like with any other programming language.

$ErrorActionPreference has to be set to ‘stop’ to stop the script from continuing to run even after the exception has occurred. You can provide a name of a service to the Get-Service command to get the details of that service. In the event of a service of the given name not being found, the exception will be caught and whatever is inside the catch block will be executed. This method can be useful when having to deal with various exceptions in your scripts.

--

--

Shammi Kolonne

Software Engineer @ WSO2 | Graduand from CSE, University of Moratuwa