How to determine which PowerShell version you are running

Ever needed to find out which PowerShell version you’re running?

Don’t be fooled by the path of the PowerShell executable which indicates version 1.0.

Fire up PowerShell and enter


PowerShell Basics: Getting help

When starting out in PowerShell, you’ll need to spend a lot of time figuring out how to use various cmdlets. Google can help you, but PowerShell already provides a cmdlet for this purpose. The cmdlet is called Get-Help and is very useful.


Note that if you’re using PowerShell 3.0, you will have to run Update-Help before any help is available, this command requires an internet connection. It must also be run from an elevated PowerShell session.

The usage of the command is: Get-Help <cmdlet>
Like this:

#Use any cmdlet you want after Get-Help
Get-Help Get-Process

Running this will give you a lot of useful information about how to use the cmdlet, including which parameters are accepted and aliases you can use for the cmdlet.

There are several switches you can add to Get-Help to get the information you want. Try the following commands to get a feel for the different options.

Get-Help Get-Process -Full
Get-Help Get-Process -Detailed
Get-Help Get-Process -Examples

If nothing else, remember this:

Perhaps the most useful usage of Get-Help is using it with the -Examples switch. This one will show you command examples to illustrate the usage.


PowerShell Basics: Primitive data types

One of the best things about PowerShell is how easy it is to use. It is a good idea to know a little bit about whats going on under the hood though. Although you can manage fine doing most things in PowerShell without thinking about data types, knowing about them will really help you understand what is going on and hopefully help you spend less time debugging your scripts.


What is a datatype?

Straight off Wikipedia: data type is a classification identifying one of various types of data.
Now that’s not really very helpful, is it?

Think of it like this:

If you have a number, lets say 2 – PowerShell would by default store that as a 32-bit integer – which is basically a fancy word for a number without decimals. Integer values cannot have decimals.

Lets try a different number, 2,4 – PowerShell would by default store that as a 64-bit value with a data type called a “Double”. Double values are for numbers with decimals, also known as precision numbers.

One of the most used data types and most significant is the “String”. A String is a collection of characters. When defining a string value, we always enclose the value in double quotes. like this:

 $String = "Hello" 

Open your shell and try the command without double quotes. Spoiler: You’ll get a big red error message.

A detailed list of data types for .NET can be found here:

Short demonstration if you haven’t already done so yourself:

Note the method gettype(). You can use that on any object to figure out the data type, it is very useful during debugging.


Usage in PowerShell

As we have already seen, PowerShell chooses the appropriate data type for us, allowing us to think about other things. But if we really want to have full control we can specify it ourselves like this:

[Int32]$number = 2
[Double]$number2 = 2.4
[String]$string = "hello"

Remember how Integers cannot have decimals? Try this and see what you get:

[Int32]$number = 2.4
Write-Host $number

Because Integers cannot have decimals, it rounds the number to the nearest non-decimal number.

Again I would like to stress the usefulness of the method gettype(). Get into the habit of using it and you won’t be wondering what data type your script is returning.


Enough boring theory! Why is this useful?

Consider the following code:

$number1 = "2"
$number2 = "2"
$number1 + $number2

Without knowledge of data types you might think the result would be 4.
However, because we enclosed the values in double quotes, the variables have become Strings instead of Integers. Add the two Strings and you get “22”.

Now consider this code:

[Int32]$number1 = "2"
[Int32]$number2 = "2"
$number1 + $number2

This time we have told Powershell to use Integers as our data types and get the expected result 4.
Note that what we are doing here is actually defining $number1 and $number2 as Strings and then converting them to Integers. This is a technique known as ” typecasting”.
If we wanted Integers we should not have used double quotes in the first place.

As we can see in the above examples, getting the type wrong can result in unexpected results.

Perhaps the most useful place to specify data type, is when defining parameters for a function. Validating input is very important and defining which data type you use will be very useful in the validation process. Think about it – When you ask for a user to enter his or her age, you expect something like 25 or 40. You don’t want your script to continue if the user types “Banana” or 42,6.

PowerShell basics: Variables

Many Windows Administrators use PowerShell for its shell purposes but anyone with a scripting or programming background will want to use it for its scripting capabilities. In this post I’ll cover the basic usage of variables in PowerShell.


What are Variables?

Variables are nothing more than placeholders for information or for a value. Each variable is defined with a name and the name is used as a reference to whatever is stored in the variable. Variables are useful to store information you want to use later, or to cache the information so you don’t have to keep querying the same information over and over again. Any type of data may be stored in a variable.


Usage in PowerShell

PowerShell variables always start with a dollar sign ($) and are followed by the variable name like this: $variable. Values are assigned using the equals operator (=). Note that variable names in PowerShell are not case-sensitive. $hello and $Hello are the same thing. People with experience from strongly typed languages may notice there is no need to explicitly define the datatype. although you could type [int]$number = 1, there is no need as PowerShell will figure it out. It is useful in certain circumstances though.
Let’s try it:

In the example above, we created a variable called $hello and assigned the value “Testing variables!”. We then created another variable called $number and assigned the value 123. Notice the arithmetic operation at the end where we used our variable $number and added 1.