Powershell datatypes

From wikinotes

variable manipulation

$cwd = get-location    # save output of cmdlet to variable
$cwd                   # print variable
$cwd | get-member      # pipe objects to get-member to present information about the object-type
$cwd.GetType()         # same as $cwd | get-member

type conversion

$array  = $env:PATH.Split(';')                                  # cannot change items
$list   = [System.Collections.ArrayList]$env:PATH.Split(';')    # can change items
$string = [String]$list                                         # convert list to a string (separated by $ofs variable value)

types

environment variables

$env:SystemRoot        # environment variables accessible under $env:

# Add to $PATH
$cmake_PATH = 'c:\program files\cmake\bin'
if (!(where.exe cmake)){
    $PATH = (Get-ItemProperty -Path 'Registry::HKCU\Environment' -Name PATH).Path.ToLower()
    if (!( $PATH.Split(';').Contains( $cmake_PATH ) )){
        echo "Adding cmake to path"
        Set-ItemProperty -Path 'Registry::HKCU\Environment' -Name PATH -Value ($PATH +";"+ $cmake_PATH)
    }
}

arrays

Arrays are fixed-size, and cannot be modified.

$array = 'a','b','c'
$array.Contains('b')
$array[0]

lists

lists are dynamically sized, and their contents can be modified.

$list = New-Object System.Collections.ArrayList
$list.Add( 'appended item' )
$list.Insert( 0, 'first item' )
$list.Remove( 'appended item' )
$list[0]

$ofs = ';'
$string = [string]$list   # convert list to a string, separated by $ofs
$ofs = ' '