PowerShell Data Types

The most common DataTypes used in PowerShell are listed below.

PowerShell has two built in variables $true and $false for displaying the true and false boolean values.
There is also [void] casting an expression to the void datatype will effectively discard it (like redirecting to $null)

Unicode

To encode a Unicode character in a PowerShell string, prefix the unicode with 0x and cast it to System.Char:

PS > [char]0x263a

Casting

To force a conversion to a specific datatype, prefix the value or variable with the type in square brackets, this is known as a Cast Operator and forces the chosen datatype:

PS C:\> [int]“0064″
64

PS C:\> [int]$false
0

PS C:\> [byte](‚0x‘ + ‚FF‘)
255

Casting is particularly important when reading in data supplied by a user (with read-host) casting to [string] will return a String even if the user enters 123

If you cast a fractional value to an integer, PowerShell will Round() the result, to strip off all decimals behind the decimal point, use Truncate() from the .NET Math library.

Casting a string into [DateTime]will by default accept USA format dates MM/DD/YYYY or ISO 8601 YYYY-MM-DD. You can override this by using ::ParseExact to specify the exact format of the date string you are supplying.

For example to cast a date string „08-12-2012“ that’s in UK format:

Cast a date string „09-Jun-2012“ that’s in UK format and then display it as „yyyy-MM-dd“

Testing DataTypes

To test the datatype of a value use a comparison operator:

 [/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]