Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, May 18, 2011

Test to see what that data type of a value is in PowerShell

PowerShell has a comparison operator called –is.  The –is operator simply response True or False when you use it to verify the data type of a value.  The valid data types in PowerShell are:
[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

Below is a script that will use –is to test some values.
$String = "Hello"
$Boolean = $True
$Int = 15

Write-Host "Test for string"
$String -is [String]
$Boolean -is [String]
$Int -is [String]

Write-Host " "
Write-Host "Test for Boolean"
$String -is [Boolean]
$Boolean -is [Boolean]
$Int -is [Boolean]

Write-Host " "
Write-Host "Test for Integet"
$String -is [Int32]
$Boolean -is [Int32]
$Int -is [Int32]





Here are the results:


Test for string

True


False


False


 
Test for Boolean


False


True


False


 
Test for Integet


False


False


True

No comments: