<# This material is used in Prakashan Korambath's PowerShell tutorial
powershell has several variable types
[int] or [int32] 32-bit signed integer[long] 64-bit signed integer[char] Unicode 16-bit character[string] Fixed-length string of Unicode characters[single] or [float] Single-precision 32-bit floating point number[double] Double-precision 64-bit floating point number[decimal] 128-bit decimal value[bool] True/false value[byte] 8-bit unsigned integer[array] Array of values[hashtable] Hashtable object [xml] or [XmlDocument] Xmldocument object[DateTime] Date and time.[TimeSpan] Time interval.[PsObject] PowerShell object.[Switch] PowerShell Switch parameter.[SctiptBlock] PowerShell Script object.[RegEx] Regular expression.[GUID] Globally unique 32-byte identifier.
#>
# Integer variables[int] $a = 1[int] $b = 2write-Host "The sum of $a and $b is" ($a + $b)Write-Host "The variable type of variable a is" $a.getType().Name
# String variable
$c="4" Write-Host "The variable type of variable c is " $c.getType().Name
# double variable[double] $e = 3.5[double] $f = 4.1
write-host "The sum of $e and $f is " ($e + $f)
[string] $var1 = 'hello'Write-Host "The variable type of variable va1r is" ($var1.getType().Name)[string] $var2 = 'world!'
$fullvar = $var1 + " " + $var2
Write-Host "The concaternation of $var1 and $var2 is " $fullvar
Write-Host "The Length of $fullvar is " $fullvar.LengthWrite-Host "The $fullvar to upper is " $fullvar.ToUpper()Write-Host "The $fullvar to lower is " $fullvar.ToLower()
$arr1 = 12, "Word" # array of System.Int32, System.String$arr2 = dir C:\Windows\System32 # FileInfo and DirectoryInfo types
Write-Host "The variable type of variable arr1 is " $arr1.getType().Name
Write-Host "The variable type of variable arr2 is " $arr2.getType().Name
Write-Host "The content of array variable arr1 is " $arr1
$city=("Los Angels","San Diego","San Francisco","Berkeley", "Irvine", "Santa Barbara") Write-Host "The content of array variable city is " $city Write-Host "The length of array variable city is " $city.Length Write-Host "The first variable on array variable city is " $city[0]Write-Host "The fourth variable on array variable city is " $city[3]
#The string is converted to a DateTime object.[datetime] $dates = "09/12/91"$dates
Write-Host "The variable type of variable dates is " $dates.getType().Name
$today=(Get-Date).tostring(“dd-MM-yyyy”)Write-Host "Today is " $today
[single] $sa = 1.0Write-Host "The variable type of variable sa is " $sa.getType().Name
[bool] $myval = 1Write-Host "The full variable type of variable myval is " $myval.GetType().FullName
# Powershell coverts data to various types (Not all will work)
$value = 4.1
$value # Displays 4.1[int32]$value # Displays 4[float]$value # Displays 4.1[string]$value # Displays 4.1[boolean]$value # Displays True[datetime]$value # Displays April 1 ....
# This script demonstrates the difference between single quotes and double quotes.
$single = '$(3 + 4)'$double = "$(3+ 4)"
Write-Output $single # Displays $(3 + 4)Write-Output $double # Displays 7
# Hashes
$pop_cal=@{"Los Angeles"=4094764;"San Diego"=1376173; "San Jose"=1023083;"San Francisco"=856095}$pop_cal
$pop_cal.Add("Fresno", 502303)Write-Host "The popultion of Los Angeles is " $pop_cal."Los Angeles"