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: a 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: http://msdn.microsoft.com/en-us/library/47zceaw7(v=vs.71).aspx
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.