PowerShell is an extremely powerful scripting language developed by Microsoft that lets you automate tasks on your computer, and manage system resources. It is built on the .NET framework and comes with a command-line interface. Whether you’re a system administrator, a developer, or an IT enthusiast looking to enhance your automation skills, this article will guide you on your journey to learning PowerShell. Let’s get started!
Understanding PowerShell
PowerShell is not just a scripting language, it’s also a shell. So it’s both a tool to execute commands and a scripting language to run / automate tasks. It is an object-oriented language which is designed for managing and automating the administrative tasks in a Windows environment, but it’s expanded beyond Windows of the past several years..
In PowerShell, you’ll come across cmdlets (command-let), which are lightweight commands used in the PowerShell environment, and variables that store values for later use. You will also find objects, which are instances of classes from the .NET framework.
Installation
PowerShell comes pre-installed with Windows, but it is also available for Mac OS and Linux. If you are using an older version of Windows or want to update to the latest version of PowerShell, visit the official Microsoft website to download and install the latest version.
Starting PowerShell
To open PowerShell on a Windows machine, simply type ‘PowerShell’ into the Start menu’s search bar, and select the PowerShell app. On Linux or macOS, open your terminal and type ‘pwsh’.
First steps
Once you have the PowerShell interface open, you’re ready to start using it, and I’d recommend you start by looking at the list of commands that you can use, and there are too many for me to include here, but let’s start with a simple example:
Get-Process
This command will return a list of all the processes running on your system, each with its associated details like the number of handles the process has, Process ID (PID), CPU time used, memory (paged and non-paged) and more.
You can filter the results by piping to another command to apply the filter:
Get-Process | Where {$_.Name -eq "Notepad"}
If you want to, you can then stop a process by using the below command:
Stop-Process <<id>>
Basic cmdlets
Cmdlets are the heart and soul of PowerShell. They follow a verb-noun pattern. For example, Get-Process
, Set-Location
, Remove-Item
, Write-Host
etc. This makes it easier to understand what a cmdlet does just by looking at its name.
Here are a few basic cmdlets you should familiarise yourself with:
Get-Help
: Used to get information about any cmdlet
Get-Command
: Lists all of the commands that are installed on the computer, including cmdlets, aliases, functions, etc
Set-Location
: Allows you to change your working directory
Get-Location
: Shows your current directory
Get-Content
: Displays the content of a file
Write-Host: Writes a message to the console
If you’re used to using the command prompt and dos commands, many of those will still work, so if it helps you to transition, take a hybrid approach as you learn PowerShell.
Variables, operators, and objects
In PowerShell, you can create variables to store values for later use. Variables are created by simply typing a dollar sign followed by the variable name, like $var
1. The below screenshot shows setting a variable and writing it to the console:
PowerShell also includes a set of operators that you can use to calculate, compare, or manipulate variables. For example, you can use +
, -
, *
, and /
to perform basic arithmetic operations, just wrap you equation in brackets, as shown below:
Because PowerShell is build on .NET Common Language Runtime (CLR), the objects in PowerShell are essentially .NET objects. This allows you to perform complex operations on them, like selecting specific properties and invoking methods.
Scripts
Scripts in PowerShell are essentially just text files that contain one or more cmdlets. You can create a script by simply creating a new text file with notepad or NotePad++, writing your cmdlets into it, and saving it with a .ps1 extension. You can then run your script directly from the PowerShell prompt.
If you want to see an example of a script, check out my download photos from Rightmove using PowerShell article.
Execution policy
You might your scripts won’t run, and if that’s the case, it’s likely down to the execution policy being set to the default restricted level. This is done to help keep you safe, as it prevents malicious scripts from being executed. You can change it, but make sure you’re comfortable with doing it before you make the change. To change it, just run Set-ExecutionPolicy (as an administrator) followed by your desired mode. The options available are:
- AllSigned: Allows scripts to run only if they are signed by a trustworthy publishers
- Bypass: Allows anything to run, with no warnings or prompts
- Restricted (default): Blocks PowerShell scripts from running, but you can still run individual commands
- RemoteSigned: As above, plus also any locally created scripts
- Undefined: You might see this, if you ad a scope, but you wouldn’t set this
- Unrestricted: Allows any script to be run, regardless of who created it
If you wanted to change your execution policy to ‘RemoteSigned’, run PowerShell as an administrator and run the below:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
You can run it without the -ExecutionPolicy, but the documentation says to use it, and it does become more relevant if you expand the command to include scope etc.
If you want to see what you execution policy is set to, you can find by using the below command:
Get-ExecutionPolicy
Final thoughts
PowerShell is an incredibly powerful tool that can help you automate tasks and manage systems more effectively. Although it may seem complex at first, once you get the hang of it, it becomes an essential language, and shell, that will help you with your day-to-day tasks and automations.
I use PowerShell to run daily tasks and to install complicated software, setup servers to a required spec and config.
Like any other skill or language, mastering PowerShell requires practice, but if you already use .NET, then it’ll be fairly easy to get used to.
Keep experimenting with cmdlets, and don’t forget to check out the Microsoft documentation, and if all else fails, Google and ChatGPT are your friends.