Beginner’s guide to Powershell

It doesn’t matter if you are a beginner or a seasoned Windows system administrator: you need to learn Powershell. It will be part of every job application and it will be part of your daily life.

PowerShell is an object-oriented automation scripting language with an interactive command-line shell. You can find it in every modern Windows OS starting with Windows 2008R2 (released in 2009).

How to open Powershell

There are two ways of working with Powershell.

  • To launch the PowerShell command line, type powershell.exe  in the Windows Start menu. You’ll see a screen like the following:

This is just an interactive commandline where you can quicly type individual commands. For example, type Get-WinSystemLocale and press enter. The response will be the language of the Windows Installation you are currently using.

  • To launch PowerShell ISE, type powershell_ise.exe  in the Windows Start menu. PowerShell ISE provides syntax highlighting, auto-filling of commands, and other automation features that simplify script development and testing.

In the top pane, called the Script Pane, you can enter your Powershell script and when executed the result or output will be shown in the bottom pane, called the Console. You can still enter commands in the Console directly, but each line will be executed immediately when you press enter.

Type Get-WinSystemLocale in the Script Pane and press Enter. To execute the script, press F5. You just ran your first Powershell script.

Powershell scripts

PowerShell scripts are stored in.ps1 files. To prevent mistakes, you cannot run a script by simply double-clicking a file. Instead, to execute a script, right-click it and click “Run with PowerShell”.

In addition, there is a policy that restricts script execution. You can check this policy by running the Get-ExecutionPolicy command in PowerShell. For more information on this topic and how to enable running downloaded Powershell scripts on your system read here.

The building blocks of Powershell: cmdlets

A cmdlet is a PowerShell command with a specific function. The three most important characteristics are:

  • Cmdlets output results as an object or as an array of objects.
  • Cmdlets can get data for analysis or transfer data to another cmdlet using pipes.
  • Cmdlets are case-insensitive.

A cmdlet always consists of a verb (or a word that functions as a verb) and a noun, separated with a hyphen (the “verb-noun” rule). For example, some of the verbs include:

  • Get – To get something, does not change the value or setting
  • Set  – To define something, makes changes to the value or setting
  • New  – To create something new
  • Remove – To remove something existing
  • … and more

Example: Get-Date will show the current time but will not change it. Set-Date will set your computer’s clock. Not all verbs exist for all nouns. There is no Remove-Date to make a blue monday disappear…

Cmdlets and Parameters

Each cmdlet has several parameters that customize what it does. The PowerShell ISE will automatically suggest all valid parameters and their types after you type a cmdlet and a hyphen (-):

Powershell pipe

Powershell has the philosophy to have specialized commands that only perform small, simple tasks. The output of every command can become the input to another. To chain commands, you use the pipe (|) symbol. The output of the command on the left will become the input of the command on the right. For example:

Get-Service = display the services installed on the computer
Sort-Object = sort the input alphabetically

Get-Service | Sort-Object

This will give you a sorted list of all the services installed on the computer.

The usage of pipe is one of the core building blocks of Powershell and it is not uncommon to use 4 or even more pipes in a single command line.

Why learn Powershell

For your daily Active Directory and Office 365 tasks (the focus of this website), you will mainly be using commands to manipulate objects in your local AD and in your Office 365 environment (Azure AD and Exchange Online). For example, with Powershell, it is quite simple to change 500 passwords for user accounts and store them in a file, or to scan your AD for inactive computer accounts. Using the GUI tools to do such a thing would require opening each user, change the password, add it to a file, etc. That would take far too long. The time you spent on learning Powershell pays itself back in no time. On top of that, there are many useful resources online where you can download Powershell scripts made by other admins. And if you need details, Microsoft has extensive documentation of every cmdlet they offer.