This is part 2 of my Beginning Powershell Scripting tutorial series. It is assumed that the reader has read and understood the material in part 1 before continuing with this section.
In this part, I am going to deviate a bit from the script-centered focus of this series. We'll be entering Powershell commands directly on the command-line. This is a great way to learn Powershell, and allows you to get immediate feedback when trying out new things. All of the commands presented here, however, will work just as well within a script.
Note: One thing you may find of help when working with the Powershell command line is that you can retrieve a complete list of defined variables by typing
get-variableat the Powershell command-prompt. This will return both the automatic variables that are declared by Powershell itself and the user variables that you've defined during your session.
Powershell allows the declaration of both implicitly-typed and explicitly-typed variables. Implicitly-typed variables may hold data of any type, and are declared without a type, as shown:
$myData = "Hello, World!"
In this case, the data type is string–it represents a sequence of characters. Unlike the values in
.bat files, however, Powershell data types can be more than strings! In fact, Powershell can use any data type that is defined by the Microsoft .NET Framework. The following declaration is also implicit, but stores a value of type double:
$myData = 1.23
$myData is an implicit type, it will hold the double value 1.23 here, even if you earlier assigned the string value “Hello, World!” to it. Implicit types are amorphic; Powershell automatically chooses the correct data type for the value provided.
There may be times when you'll want to define exactly what type of data your variable holds. For example, you may want to store the value 1.23 as a float instead of a double. There are two ways to do this: to declare the type of the variable itself, or to declare the type of the variable's data.
When you declara a variable's data type, you create an explicit data type which will hold data only of the type you declare it to be. If you want
$myData to hold only floating-point data, declare it like this:
[float]$myData = 1.23
$myData will hold only float data. If you try to set
$myData to a value that can't be automatically converted to a float, the assignment will fail loudly. For example:
PS C:\bin> [float]$myData = 1.23 PS C:\bin> $myData = "Hello, World!" Cannot convert value "Hello, World!" to type "System.Single". Error: "Input string was not in a correct format." At line:1 char:8 + $myData <<<< = "Hello, World!" + CategoryInfo : MetadataError: (:) , ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException
To change the type of
$myData, you'll need to re-declare the variable's type. In this example, we change
$myData from a float to a string:
PS C:\bin> [float]$myData = 1.23 PS C:\bin> [string]$myData = "Hello, World!"
You can also choose to declare the data's type instead. This allows your variables to hold any data type, but gives you strict control over the type that is assigned for the value you provide. As an example, notice the difference between these two assignments of the value “01/28/1969”.
PS C:\bin> $myData = "01/28/1969" PS C:\bin> $myData 01/28/1969 PS C:\bin> $myData = [datetime]"01/28/1969" PS C:\bin> $myData Tuesday, January 28, 1969 12:00:00 AM
In the first case, the value was not explicitly defined, and was automatically interpreted by Powershell as a string value. To store the same value as a datetime value, you must declare it as such.
Note: This operation is also known as casting, and is one way to force Powershell to interpret a given value or variable as a particular type. For another, read on!
Powershell defines a number of operators that are useful in comparing and converting data types:
| || Returns True if the data in
| || Returns True if the data in
| || Returns the data in
-is and -isnot are used to compare a variable or data literal with a given type. If you want to see if the value 1.23 is stored as a float or double, you can check against each:
PS C:\bin> $myData = 1.23 PS C:\bin> $myData -is [float] False PS C:\bin> $myData -is [double] True
Similarly, you can check to see if data isn't of a particular type:
PS C:\bin> $myData = "01/28/1969" PS C:\bin> $mydata -isnot [string] False PS C:\bin> $mydata -isnot [datetime] True
You can use -as when you want to convert data to a given type. Powershell will do its best to convert it to the type you specify. For example:
PS C:\bin> $myData = 1.23 PS C:\bin> $myData -as [int] 1 PS C:\bin> $myData -as [datetime] Friday, January 23, 2009 12:00:00 AM PS C:\bin> $myData -as [bool] True PS C:\bin> $myData -as [string] 1.23
This is the same as the data casting operation, above. In fact, the above lines can be written equally as:
PS C:\bin> $myData = 1.23 PS C:\bin> [int]$myData 1 PS C:\bin> [datetime]$myData Friday, January 23, 2009 12:00:00 AM PS C:\bin> [bool]$myData True PS C:\bin> [string]$myData 1.23
That's it for now! In a future article, I will talk about decision-making and looping.
More information about Powershell data types can be found here:
Get help about operators
Type this at the Powershell prompt for built-in help regarding the type operators shown above:
View the built-in types table in the C# reference
The Built-In Types Table (C# Reference) provides a list of data types used in C#. Not coincidentally (since both C# and Powershell are designed for the .NET Framework), these types are the same ones that are used in Powershell.