Finally decided to learn a little Powershell

For years I’ve been thinking of learning Powershell, but I’ve never really had a real need for it, so subsequently it got neglected for such a long time.

I’ve finally decided to spend some time with it and see what it can do for me. Let’s start by looking at some of the basics to getting started with Powershell.

Commands (or Cmdlets)

Built-in commands are also known as Cmdlets, although I’ve also seen that some users prefer to think of Cmdlets as multiple commands – more like batch files. Either way, I’ll use the word interchangeably.

Note: Commands are case-insensitive

Let’s jump right in

Let’s have a look at an example command in Powershell

Get-Process | where {$_.CPU -gt 1000}

In the above, the command is Get-Process. This returns a list of the current processes running on your computer. Running Get-Process will result in eight headings (on my machine). Get-Process actually returns data as objects which we then pipe using | to the where clause.

The where clause syntax uses the braces {} to group together to form a block (much like blocks in C++ etc.). Within this block we use the $_ syntax which is a placeholder which in essence means “the current value” from the Get-Process command. In other words each item return from Get-Process is piped to the where clause and the $_ indicates the current value. From this we use standard object oriented style dot notation to get at a property on the item $_, in this case the property is CPU.

Note: The $ is used for to denote a variable, whether it’s supplied by default by Powershell or created by the user

Next up with have the -gt which as I’m sure you can determine, means “greater than” and ofcourse on the right hand side of this expression is the value we want to test for.

How do we determine what properties exist on an object?

In the previous command we used the CPU property of the results from Get-Process but how did we know that property existed and are there more properties we could have used? Ofcourse, there’s documentation which can tell us such things, but we can also use another Powershell command to tell us.

By typing the following

Get-Process | Get-Member

We can pipe the Get-Process object into Get-Member which will output a list of properties (and other members) of the type returned by Get-Process. If you run this you’ll find the CPU property (and many others).

Actually using the Powershell command prompt we can also view the properties by pressing ctrl+space after the dot, this allows us to list all available member names for an object. In the case of Get-Process this will list 122 possible member names.

Getting help

Powershell’s help system is very powerful. Simply type

Get-Help

to view the help on the Get-Help command. From here you’ll see you can type

Get-Help Get-Process

which will display help on the Get-Process command. We can also view examples for this command using

Get-Help Get-Process -examples

Aliases

Some commands are also aliased, meaning our original Get-Process | where {$_.CPU -gt 1000} can be rewritten as

ps | where {$_.CPU -gt 1000}

ps is simply an alias to the longer winded Get-Process and we can find an alias (if one exists) using the following

Get-Alias -definition Get-Process

Ctrl+Space

I noted previously that you can get a list of member names by pressing the combination ctrl+space after the dot, this is also useful after the – operator, for example when looking for alternatives to gt we can type – then ctrl+space and a list of options appears which includes, bnot, eq, f and more.