PowerShell Data Types

The most common DataTypes used in PowerShell are listed below.

 [string]    Fixed-length string of Unicode characters
 [char]      A Unicode 16-bit character
 [byte]      An 8-bit unsigned character

 [int]       32-bit signed integer
 [long]      64-bit signed integer
 [bool]      Boolean True/False value

 [decimal]   A 128-bit decimal value
 [single]    Single-precision 32-bit floating point number
 [double]    Double-precision 64-bit floating point number
 [DateTime]  Date and Time

 [xml]       Xml object
 [array]     An array of values
 [hashtable] Hashtable object

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 out-null or 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

When assigning a value to a variable, you can cast either side of the expression so

$variable = [int]0123

is not quite the same as

[int]$variable = 0123

When a value is cast to a particular datatype, it is a one time thing.
When a variable is cast to a particular datatype, it will stick just like DIM Variable As DataType in other languages.
The variable's datatype will not change again unless you explicitly recast the variable to a different data type, or remove the variable completely (with Remove-Variable) and re-create it.
Notice that Get-Member and GetType() will display the current datatype but do not indicate if it is static or variant.

[int]$test = 0123
$test = [string]"00456"
# at this point, which data type do we end up with?
$test.GetType().FullName

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:

PS C:\> [DateTime]::ParseExact("08-12-2012","dd-MM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture) 
Saturday, December 08, 2012 00:00:00

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

PS C:\> [DateTime]::ParseExact("09-Jun-2012","dd-MMM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture).ToString("yyyy-MM-dd")
2012-06-09

Testing DataTypes

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

PS C:\> 32 -is [int]
True PS C:\> $true -is [bool]
True

If you are going to cast a variable into a new datatype it is a good idea to first test if the value will cast sucessfully using -as, if the conversion fails, it will return a NULL, which can be used to either avoid the conversion or to flag an error:

[string]$var = "64a"
if( ($var -as [double]) -ne $null ) {
   [double]$var = $var
}
$var.GetType().FullName

“Character is what we do when no one's looking” ~ Bill Hybels

Related PowerShell Commands:

Variables and Operators - Create, add, subtract, divide.
DateTime formats
Get-Item Variable:
Get-Variable


Copyright © SS64.com 1999-2018
Some rights reserved