A Guide to PowerShell – part 1

Welcome to part 1 of 3 of The Solving A guide to PowerShell. Click here for Part 2 and Part 3.

Windows PowerShell is a vast subject, the aim of this guide is to provide system administrators a simple, usable guide to the fundamentals of PowerShell. We will also demonstrate scripts and commands which can be used in typical day-to-day tasks. This guide will give you the basic steps to get you started, it will teach you the core skills needed to open the door to PowerShell and object oriented scripting.

What is PowerShell?

Windows PowerShell is a command line shell, a scripting language and an automated task framework from Microsoft. It is based on .NET framework and is great at automating batch jobs and system tasks. PowerShell is available on both Windows and Linux platforms, and has been integrated into Microsoft operating systems since Windows Server 2003 R2. Today, PowerShell is installed by default on all Microsoft Server releases, and all professional versions of their modern desktop operating systems. Additionally you can download it direct from the Microsoft website if required.

PowerShell is not a compiler programming language like C, it is an interpreted scripting language. It comes with a number of set commands (cmdlets) which can be executed by the user against a whole range of different variables. This is great for interrogating and executing commands against an entire datacenter of servers at once. PowerShell is also a command line executable, you can type almost any Windows command into PowerShell and it will act just like the Command Prompt would.

How to get PowerShell?

Before we start, it is recommended that you are on the latest version of PowerShell (5.0 or above). With every release, new functions and cmdlets are released, and as PowerShell is an evolving scripting language, new features are being added allowing better integration and services.

Windows PowerShell includes the Core Shell and PowerShell ISE (Integrated scripting environment) applications. These can be found from your start button by typing “PowerShell“. It is always recommended to run PowerShell as administrator.

If you need to upgrade your version, follow the first part of this article (Only sections 1 to 10 apply).

You can check which version of PowerShell you are running by opening the Shell and typing $psversiontable  – look for the property PSVersion

Powershell - PSVersiontable

PowerShell Basics and Get-Help

One of the first concepts to understand is the PowerShell help system. PowerShell has been designed from the ground up to be as simple to use as possible. Each cmdlets contains a detailed help file explaining how to use the cmdlet.

The help file includes instruction on the command description, what syntax to use and even an example of how to use the command.

The PowerShell community has also embraced this self-documenting approach and the vast majority of online scripts have detailed help files. It is important to get into the habit of always checking the help files, even if you are not stuck. You will find out so much about a cmdlet and ways in which you can use it from the help files.

  • First you should update your help files from Microsoft by typing update-help – this will instruct powershell to connect to the internet and download all the latest help files. This process can take several minutes but only needs to be run once.
  • Next type Get-Command – this will list you every single command available in your version of PowerShell. (PowerShell Tip – Command grammar is always singular: Log not Logs; Service not Services)
  • Nearly every help file shows examples – If you use the command get-help get-service -examples you will be able to see examples of the command in use. This is a perfect way for users to learn the commands and grasp how to construct cmdlets.

get-help get-service

  • Above, you can see the help file for get-service, four key areas have been highlighted
    • SYNOPSIS – This tells you what the command does
    • SYNTAX – This tells you how you use the command and construct cmdlets
    • RELATED LINKS – This tells you other similar commands or related commands, in this example, this is commands that get-service can interact with in the pipeline.
    • REMARKS – This tells you how you can get more information about the command.

Set-ExecutionPolicy

The Execution policy is a security element of Windows PowerShell which determines if the user can execute commands and load configuration files. Essentially if you have this set incorrectly you will not be able to execute all scripts in your version of PowerShell. This becomes more apparent when using scripts downloaded from the internet and the policy is in place to protect your system.

There are several ways you can change this policy, the most common way is to just allow scripts to be executed, or if preferred, you can simply turn off the security policy. The second approach may not be best practice, but it is extremely useful in training and test labs.

  1. To enable the running remote scripts type set-executionpolicy remotesigned
  2. To switch off the policy type Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Use option 2 with caution and never in a production environment.

 

What is the Pipeline?

The pipeline is used to import, export and convert data in a Windows PowerShell command. It is important to always think “What is in the pipeline?” when executing commands. If you are experienced in Linux, this concept may be easier to grasp. Each time you execute a PowerShell command, the results (or data) is passed down the pipeline. The resulting data can then be used to execute more commands, and so on and so on.

Note – Commands are split using the pipe | symbol.

The easiest way to explain is to take a simple command, for example get-process | stop process

The result of Command A (get-process) is passed down the pipeline to Command B (stop-process) which is then passed to Output. You can therefore manipulate Command A to pass down specific results. You can also pipe as many of your results as you want to create very granular cmdlets.

Powershell Fundamentals

Windows PowerShell is an object oriented shell, each cmdlet is a collection of objects, each object has its own properties. This can be demonstrated by using the get-member command. You can pipe each command to get-member eg. get-service | get-member. 

To explain this further, type get-service into the shell

 

get-service

You will see information on each service on your system including status, name and displayname. But, there are many more properties for the object get-service than you are seeing here.

Now type get-service | get-member

This will output the objects applicable to the Get-Service dataset including Alias, Methods and property.

 

get-process | get-member

You can now use the information from Get-Member to build out your commands.

Now type get-service | Select-object Name, startype, status

You will see information about your system services, this time displaying Name, Starttype and Status.

This can be taken further by introducing sorting, grouping and measuring.

Now type get-service | Select-Object Name, Starttype, status | sort-object StartType

This will sort your results by Starttype. (Default is alphanumerically)

Powrshell sorting

Now type get-service | Select-object Name, StartType, Status | Group-Object -property Status

This will group the output and show you how many services are Running, Stopped or if applicable disabled. It will automatically Count them for you as well. The data in the parentheses {} indicates more than one object has the value.

get-service - group

Finally, now type Get-service | Where-object status -eq “Running” | Measure-object

Where-object | MEasure-Object

This command will only look for Running services and counts them. Here we have 80 running services.

 

In part 1 we have just scratched the surface of Windows PowerShell. It is important to remember the fundamentals introduced here. Check Part 2 of The Solving A Guide to PowerShell where we will introduce Importing and Exporting Data.

 

Read related articles