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 redirecting to $null)
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"
PS C:\> [int]$false
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:
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
To test the datatype of a value use a comparison operator:
PS C:\> 32 -is [int] -> true PS C:\> $true -is [bool] -> true
In addition to the above, if you are using other .NET classes then you can use those DataTypes too.
“Character is what we do when no one's looking” ~ Bill Hybels
Related:
Variables and Operators - Create, add, subtract, divide.
DateTime formats
Get-Item Variable:
Get-Variable
© Copyright SS64.com 1999-2013
Some rights reserved