Windows/PowerShell

Windows PowerShell
Windows PowerShell - http://www.microsoft.com/powershell

Wikipedia - Windows PowerShell
Windows PowerShell - Wikipedia - http://en.wikipedia.org/wiki/Windows_PowerShell

Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework. PowerShell provides full access to COM and WMI, enabling administrators to perform administrative tasks on both local and remote Windows systems.

In PowerShell, administrative tasks are generally performed by cmdlets (pronounced command-lets), specialized .NET classes implementing a particular operation. Sets of cmdlets may be combined together in scripts, executables (which are standalone applications), or by instantiating regular .NET classes (or WMI/COM Objects). These work by accessing data in different data stores, like the filesystem or registry, which are made available to the PowerShell runtime via Windows PowerShell providers.

Windows PowerShell also provides a hosting mechanism with which the Windows PowerShell runtime can be embedded inside other applications. These applications then leverage Windows PowerShell functionality to implement certain operations, including those exposed via the graphical interface. This capability has been utilized by Microsoft Exchange Server 2007 to expose its management functionality as PowerShell cmdlets and providers and implement the graphical management tools as PowerShell hosts which invoke the necessary cmdlets. Other Microsoft applications including Microsoft SQL Server 2008 also expose their management interface via PowerShell cmdlets. With PowerShell, graphical interface-based management applications on Windows are layered on top of Windows PowerShell. A PowerShell scripting interface for Windows products is mandated by the Common Engineering Criteria.

Windows PowerShell includes its own extensive, console-based help, similar to man pages in Unix shells via the Get-Help cmdlet

Installation
Download Windows PowerShell 2.0 - http://support.microsoft.com/kb/968929
 * Download the Windows Management Framework Core (WinRM 2.0 and Windows PowerShell 2.0)
 * Install Windows PowerShell 2.0 - http://msdn.microsoft.com/en-us/library/ff637750.aspx

OLD: How to Download Windows PowerShell 1.0 - http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx

Editor
From PowerShell 1.0: C:\WINDOWS\system32\windowspowershell\v1.0\powershell_ise.exe

Windows PowerShell Quick Reference
A two-page quick (.doc) reference guide to using Windows PowerShell.

TechNet Scripting with Windows PowerShell
This page brings together resources for system administrators who are interested in learning about the Windows PowerShell command line and scripting environment.

Comparison of cmdlets with similar commands
The following table contains a selection of the cmdlets that ship with PowerShell noting the most similar commands in other well known command line interpreters.

Security
PowerShell Cookbook - Get rid of the “execution of scripts is disabled” message - http://www.johndcook.com/PowerShellCookbook.html#a4

PSH> .\myScript.ps1

File D:\myScript.ps1 cannot be loaded. The file D:\myScript.ps1 is not digitally signed. The script will not execute on the system. Please see "get-help about_signing" for more details..

At line:1 char:13 + .\myScript.ps1 <<<<

PSH> set-executionPolicy RemoteSigned

Better option: (you will get a confirmation warning with this mode) PSH> set-executionPolicy Unrestricted

To really unsecure the system: PSH> set-executionPolicy ByPass

Run script in ByPass mode: powershell -ExecutionPolicy ByPass -File script.ps1

WARNING: This has to be set for each environment separately. (32bit vs 64bit)

References and Tutorials

 * Windows PowerShell - http://technet.microsoft.com/en-us/library/ee692944.aspx
 * PowerShell.nu » Running Scripts with arguments in PowerShell - http://www.powershell.nu/2009/12/16/running-scripts-with-arguments-in-powershell/
 * Powershell Beginner Tutorial « Tome's Land of IT - http://powertoe.wordpress.com/category/powershell/powershell-beginner-tutorial/

Help
To see the examples, type: "get-help Get-Random -examples". For more information, type: "get-help Get-Random -detailed". For technical information, type: "get-help Get-Random -full".

Version
To see the version of powershell: PS> $Host.Version Major Minor  Build  Revision - -  -  2      0      -1     -1

comments
echo "hello" # this is a comment
 * 1) this is a comment

echo
echo "Hello $name" echo "Hello xxx$($name)xxx" Write-Output "Hello $name" "Hello ${name}" # same effect Write-Output -NoNewline "no newline"

$a = "Hello " $b = "World!" echo ($a + $b)

Write-Host -ForegroundColor green "Message" # write message in color

Path
echo $env:Path $env:Path = "${env:Path};C:\Python27;C:\Python27\Scripts"

Variables
$joe = 5 $joe = "abc" echo $joe echo --${joe}--

Get variable type: $var.GetType.Name $var.GetType.FullName

Variable set: if ( !$var ) { $var = "new-value"; } if ( $var -eq $null ) { $var = "new-value"; }

Arguments
"Arguments: $($args.count)" $args # list all args

$args[0] # is first argument arg1 = $args[0] arg2 = $args[1] echo "$arg1 $arg2"

param($Argument1,$Argument2) "Argument1 is: $Argument1" "Argument2 is: $Argument2"

Strings
Check for empty string: if ($str) { "Not empty"}

Join operation: $a = "This", "Is", "a", "cat"


 * 1) Operator join

# This Is a cat $a -join ' '

# ThisIsacat -join $a


 * 1) using conversion to [string] (and optionally use the separator $ofs)

# This Is a cat [string]$a

# This-Is-a-cat $ofs = '-' # ! after that all casts work in this way until $ofs changes ! [string]$a

$var.tostring [string]$var1 = 5 # cast to string

Trim: $msg = " Hello world " $msg.Trim

Comparison: if ( $str1 -eq $str2 ) { ... } if ( $str1.startswith('something') ) { ... }

Variable Expansion: "This is expansion: $($obj.Name)"

Exit Code
exit 1

If
PowerShell Tutorial - Conditional Logic | PowerShell Pro! - http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-conditional-logic/

if (! $args[0]) { "Error: missing parameter" exit 1 }

if ($args[1]) { $name = $args[1] }

Else: if (...) {} else {}

Comparision: -eq -ne -lt -gt -le -ge

if ($name -eq "joe") { ... } if ($code -ne 0) { ... } if ($rc -eq $true) { ... }

Boolean Logic: -and -or

Null: if ($myvar -eq $null) { ... }

Return Codes
Return codes: echo $?
 * 1) True - on success
 * 2) False - on fail

Dates
$Year = Read-Host "When were you born?" $Numerical_Years = (Get-Date -uformat "%Y") -$Year

Select Columns
Select all columns: pwd | select *

Select drive: pwd | select Drive

for
for ($i=1; $i -lt 6; $i++) { "This is line number " + $i }

foreach
foreach ($item in $items) { $item }

while
while ($true) { ... }

Aliases
To see a list of all the cmdlet aliases: get-alias

Booleans
$true  # echo as 'True' $false # echo as 'False'

if ($rc -eq $true) { ... } if ($rc -eq $false) { ... } if ($rc -ne $true) { ... }

Arrays
a.k.a. Lists

$empty_array = @ # does not actually appear to create an object $items = 1,"Hello",3.5 $items = @(1,"Hello",3.5) # explicit syntax $item1,$item2,$items_remaining = $items # distribute values $numbers = 1,2,3,4,5,6,7 $numbers = (1..7) $items # list all items $var = $items[0] # first element $vars = $items[2..3] # items from index 2 to 3 $vars = $items[-1] # last element $item[2] = 64 # modify value (index must exist) $item.SetValue(2, 64) # modify value (index must exist) $items += 25 # append new value $new_array = $first_array + $second_array # append arrays $count = $items.length-1 foreach ($item in $items) {$item} # loop through array $items.gettype # property BaseType will show System.Array

$array = @("test1", "test2", "test3") for ($i=0; $i -lt $array.length; $i++) { $array[$i] } foreach ($element in $array) { $element } $array |foreach { $_ }

Referneces:
 * Powershell Arrays | SS64.com - http://ss64.com/ps/syntax-arrays.html

Hash Tables
a.k.a. Dictionaries

$states = @{"Washington" = "Olympia"; "Oregon" = "Salem"; California = "Sacramento"} $states # list states $states.Add("Alaska", "Fairbanks") $states["Alaska"] = "Fairbanks" # same effect $states.Remove("Alaska") $states.Set_Item("Alaska", "Juneau") $states.Get_Item("Oregon") $states.ContainsKey("Oregon") $states.ContainsValue("Salem") $states | Sort-Object $states.GetEnumerator | Sort-Object Name $states.GetEnumerator | Sort-Object Value -descending

References:
 * Windows PowerShell Tip: Working with Hash Tables - http://technet.microsoft.com/en-us/library/ee692803.aspx

Splat Operator
$params = @{path = "c:\demo"; Recurse= $true} dir @params

That will in effect run: dir -Path c:\demo -Recurse:$true

Here Strings
$myHereString = @' some text with "quotes" and variable names $printthis some more text '@

Function
Function Add { param($x, $y) $ans = $x + $y }

Function Add { "params: $args" } Function Add { $args[0] + $args[1] } Function Age { Param([int]$x) } # type casting

Objects
pwd (alias for Get-Location) returns an object describing the current working directory: > pwd | select *

PowerCLI C:\Windows\system32> pwd | select *

Drive              Provider            ProviderPath        Path -                                  C                   Microsoft.PowerS... C:\Windows\system32 C:\Windows\system32

filter a column: $obj | select [COLUMN]

Filter records (row): $obj | Where-Object {$_.Name -eq "Something"} $obj | Where-Object {$_.Name.StartsWith("something")}

See

To get a sub object: $drive = (pwd).Drive pwd | Select-Object Drive

To get a particular property: $drive = (pwd).Drive.Name # Get drive letter $path = pwd | select -exp Path

Get Object Members: pwd | GetMember pwd | gm # equivalent

Record count: ($obj | measure-object).count # or $obj_measure = $obj | measure-object $obj_measure.count

Where-Object
Filter data with Where-Object

-lt -- Less than -le -- Less than or equal to   -gt -- Greater than -ge -- Greater than or equal to   -eq -- Equal to    -ne -- Not equal to    -like - Like; uses wildcards for pattern matching

Where-Object {$_.Name -eq "Something"} Where-Object {$_.Name -like "*th*"}

Try Catch Exception Handling
try { # code throw 'Error' exit 0 } catch { # $_ will contain details echo "Exception: " $_ # will show 'Error' exit 1 # failure }

Stop on Error
To make powershell stop executing whenever it encounters error, set $ErrorActionPreference = "Stop" # or Get-Process -ErrorAction Stop

Redirection
null: echo "Hello world" > $null  # redirect to like /dev/null
 * 1) technically any non assigned variable will work the same

STDOUT: Write-Output "Hello World" echo "Hello world" > $myfile

STDERR: Write-Error "Some Error" Write-Error "Some Error" 2> $myfile Write-Error "Some Error" 2>&1

Warning: Write-Warning "Some Warning" Write-Warning "Hidden Warning" -WarningAction SilentlyContinue # supress warnings

Streams
Capture Warning, Verbose, Debug and Host Output via alternate streams: (available in PowerShell 3.x) 0 - stdin 1 - stdout (write-output, normal cmdlet output, non captured expression output) 2 - stderr (write-error, throw, cmdlet non-terminating errors) 3 - warnings (write-warning, cmdlet warning) 4 - verbose (write-verbose and cmdlet -verbose output) 5 - debug (write-debug and cmdlet debug output) 6 - host (write-host, read-host, explicit out-host use) 9 - combined (all output combined into a single - easy to redirect stream)

capture *ALL* output to a log file MyLongRunningComplexUnattendedScript.ps1 9>&1 > LogFile.log

References:
 * Capture Warning, Verbose, Debug and Host Output via alternate streams | Microsoft Connect - http://connect.microsoft.com/PowerShell/feedback/details/297055/capture-warning-verbose-debug-and-host-output-via-alternate-streams

Progress Bar
Write-Progress for ($i = 1; $i -le 100; $i++ ){write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;}

References:
 * Technet Write-Progress - http://technet.microsoft.com/en-us/library/hh849902.aspx

Snapin
if ( (Get-PSSnapin -Name MySnapin -ErrorAction SilentlyContinue) -eq $null ) {   Add-PsSnapin MySnapin }

References:
 * How to check if PowerShell snap-in is already loaded before calling Add-PSSnapin - Stack Overflow - http://stackoverflow.com/questions/1477994/how-to-check-if-powershell-snap-in-is-already-loaded-before-calling-add-pssnapin

Read-Host - Get Input From User
$name = Read-Host "name"
 * 1) name:

Read-Host "Press enter key to continue"

$fname = [Console]::ReadLine

Password: $pass = Read-Host 'What is your password?' -AsSecureString $plan_text_pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(   [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))

Confirm: start calc gps calc | stop-process -confirm  # firm calc process, and confirm kill

Steal pop up dialog box from visual basic:  (did not work for me!) $a = new-object -comobject MSScriptControl.ScriptControl $a.language = "vbscript" $a.addcode("function getInput getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" ) $b = $a.eval("getInput")

Input Box (works): [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a computer name", "Computer", "$env:computername")

All user input and output normally occurs inside the PowerShell console. Simply access the .NET framework directly if you'd like to get back the same dialogs VBScript scripters are using. Here is the old-fashioned InputBox:

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "$env:username") "Your name is $name"

An Easy InputBox - And here is your MsgBox: [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $result = [Microsoft.VisualBasic.Interaction]::MsgBox( `  "Do you agree?", 'YesNoCancel,Question', "Respond please") switch ($result) { 'Yes'		{ "Ah good" } 'No'		{ "Sorry to hear that" } 'Cancel'	{ "Bye..." } }

Clear-Host - Clear Screen
Clear-Host

Get-Content - Get File Content
Get file content: Get-Content [file]

Aliases:
 * cat
 * type
 * gc

Get-Content dictionary.txt | foreach {Write-Output $_} Get-Content dictionary.txt | foreach {Write-Output $_.length} Get-Content dictionary.txt | foreach {Write-Output $_.toupper} Get-Content dictionary.txt | foreach {$_.length} # Write-Output is implied. $content = Get-Content dictionary.txt (Get-Content test.txt)[3 .. 5] # get lines 3 through 5 of file foreach ($line in Get-Content test.txt) { echo $line } # read each line of a file

References:
 * Using the Get-Content Cmdlet - http://technet.microsoft.com/en-us/library/ee176843.aspx
 * Windows PowerShell Tip: Reading is Fundamental - http://technet.microsoft.com/en-us/library/ee692806.aspx]

Start-Sleep - Pause Script
Suspends the activity in a script or session for the specified period of time: Start-Sleep [seconds]

Alias:
 * sleep

Get-Random - Generate Random Numbers
Generate a number within the range of 100 to 200 (including both) Get-Random -minimum 100 -maximum 200

Generate 10 numbers within the range of 1 to 100 (including 1 and 100). Numbers can only be used once, so if the count is higher than the InputObject range, you won't get the full count. Get-Random -Count 10 -InputObject (1..100)

Console Redirect
console.ps1 while ($true) { Read-Host }

Usage: (end with [ctrl]+[c]) ./console.ps1 > file

Powershell Command Line Options
C:\> powershell.exe /?

PowerShell[.exe] [-PSConsoleFile | -Version ] [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive] [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}] [-WindowStyle ] [-EncodedCommand ] [-File  ] [-ExecutionPolicy ] [-Command { - |  [-args ] | [] } ]

PowerShell[.exe] -Help | -? | /?

-PSConsoleFile Loads the specified Windows PowerShell console file. To create a console file, use Export-Console in Windows PowerShell.

-Version Starts the specified version of Windows PowerShell.

-NoLogo Hides the copyright banner at startup.

-NoExit Does not exit after running startup commands.

-Sta Start the shell using a single-threaded apartment.

-NoProfile Does not use the user profile.

-NonInteractive Does not present an interactive prompt to the user.

-InputFormat Describes the format of data sent to Windows PowerShell. Valid values are "Text" (text strings) or "XML" (serialized CLIXML format).

-OutputFormat Determines how output from Windows PowerShell is formatted. Valid values are "Text" (text strings) or "XML" (serialized CLIXML format).

-WindowStyle Sets the window style to Normal, Minimized, Maximized or Hidden.

-EncodedCommand Accepts a base-64-encoded string version of a command. Use this parameter to submit commands to Windows PowerShell that require complex quotation marks or curly braces.

-File Execute a script file.

-ExecutionPolicy Sets the default execution policy for the session.

-Command Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt, and then exits, unless NoExit is specified. The value of Command can be "-", a string. or a   script block.

If the value of Command is "-", the command text is read from standard input.

If the value of Command is a script block, the script block must be enclosed in braces ({}). You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script block are returned to the parent shell as deserialized XML objects, not live objects.

If the value of Command is a string, Command must be the last parameter in the command, because any characters typed after the command are interpreted as the command arguments.

To write a string that runs a Windows PowerShell command, use the format: "& { }"   where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

-Help, -?, /? Shows this message. If you are typing a PowerShell.exe command in Windows PowerShell, prepend the command parameters with a hyphen (-), not a forward slash (/). You can use either a hyphen or forward slash in Cmd.exe.

EXAMPLES PowerShell -PSConsoleFile SqlSnapIn.Psc1 PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML PowerShell -Command {Get-EventLog -LogName security} PowerShell -Command "& {Get-EventLog -LogName security}"

# To use the -EncodedCommand parameter: $command = 'dir "c:\program files" ' $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) powershell.exe -encodedCommand $encodedCommand

Cmdlets Aliases
PS C:\> Get-Alias

CommandType    Name                            Definition ---                                -- Alias           %                               ForEach-Object Alias          ? Where-Object Alias          ac                              Add-Content Alias          asnp                            Add-PSSnapIn Alias          cat                             Get-Content Alias          cd                              Set-Location Alias          chdir                           Set-Location Alias          clc                             Clear-Content Alias          clear                           Clear-Host Alias          clhy                            Clear-History Alias          cli                             Clear-Item Alias          clp                             Clear-ItemProperty Alias          cls                             Clear-Host Alias          clv                             Clear-Variable Alias          compare                         Compare-Object Alias          copy                            Copy-Item Alias          cp                              Copy-Item Alias          cpi                             Copy-Item Alias          cpp                             Copy-ItemProperty Alias          cvpa                            Convert-Path Alias          dbp                             Disable-PSBreakpoint Alias          del                             Remove-Item Alias          diff                            Compare-Object Alias          dir                             Get-ChildItem Alias          ebp                             Enable-PSBreakpoint Alias          echo                            Write-Output Alias          epal                            Export-Alias Alias          epcsv                           Export-Csv Alias          epsn                            Export-PSSession Alias          erase                           Remove-Item Alias          etsn                            Enter-PSSession Alias          exsn                            Exit-PSSession Alias          fc                              Format-Custom Alias          fl                              Format-List Alias          foreach                         ForEach-Object Alias          ft                              Format-Table Alias          fw                              Format-Wide Alias          gal                             Get-Alias Alias          gbp                             Get-PSBreakpoint Alias          gc                              Get-Content Alias          gci                             Get-ChildItem Alias          gcm                             Get-Command Alias          gcs                             Get-PSCallStack Alias          gdr                             Get-PSDrive Alias          ghy                             Get-History Alias          gi                              Get-Item Alias          gjb                             Get-Job Alias          gl                              Get-Location Alias          gm                              Get-Member Alias          gmo                             Get-Module Alias          gp                              Get-ItemProperty Alias          gps                             Get-Process Alias          group                           Group-Object Alias          gsn                             Get-PSSession Alias          gsnp                            Get-PSSnapIn Alias          gsv                             Get-Service Alias          gu                              Get-Unique Alias          gv                              Get-Variable Alias          gwmi                            Get-WmiObject Alias          h                               Get-History Alias          history                         Get-History Alias          icm                             Invoke-Command Alias          iex                             Invoke-Expression Alias          ihy                             Invoke-History Alias          ii                              Invoke-Item Alias          ipal                            Import-Alias Alias          ipcsv                           Import-Csv Alias          ipmo                            Import-Module Alias          ipsn                            Import-PSSession Alias          ise                             powershell_ise.exe Alias          iwmi                            Invoke-WMIMethod Alias          kill                            Stop-Process Alias          lp                              Out-Printer Alias          ls                              Get-ChildItem Alias          man                             help Alias          md                              mkdir Alias          measure                         Measure-Object Alias          mi                              Move-Item Alias          mount                           New-PSDrive Alias          move                            Move-Item Alias          mp                              Move-ItemProperty Alias          mv                              Move-Item Alias          nal                             New-Alias Alias          ndr                             New-PSDrive Alias          ni                              New-Item Alias          nmo                             New-Module Alias          nsn                             New-PSSession Alias          nv                              New-Variable Alias          ogv                             Out-GridView Alias          oh                              Out-Host Alias          popd                            Pop-Location Alias          ps                              Get-Process Alias          pushd                           Push-Location Alias          pwd                             Get-Location Alias          r                               Invoke-History Alias          rbp                             Remove-PSBreakpoint Alias          rcjb                            Receive-Job Alias          rd                              Remove-Item Alias          rdr                             Remove-PSDrive Alias          ren                             Rename-Item Alias          ri                              Remove-Item Alias          rjb                             Remove-Job Alias          rm                              Remove-Item Alias          rmdir                           Remove-Item Alias          rmo                             Remove-Module Alias          rni                             Rename-Item Alias          rnp                             Rename-ItemProperty Alias          rp                              Remove-ItemProperty Alias          rsn                             Remove-PSSession Alias          rsnp                            Remove-PSSnapin Alias          rv                              Remove-Variable Alias          rvpa                            Resolve-Path Alias          rwmi                            Remove-WMIObject Alias          sajb                            Start-Job Alias          sal                             Set-Alias Alias          saps                            Start-Process Alias          sasv                            Start-Service Alias          sbp                             Set-PSBreakpoint Alias          sc                              Set-Content Alias          select                          Select-Object Alias          set                             Set-Variable Alias          si                              Set-Item Alias          sl                              Set-Location Alias          sleep                           Start-Sleep Alias          sort                            Sort-Object Alias          sp                              Set-ItemProperty Alias          spjb                            Stop-Job Alias          spps                            Stop-Process Alias          spsv                            Stop-Service Alias          start                           Start-Process Alias          sv                              Set-Variable Alias          swmi                            Set-WMIInstance Alias          tee                             Tee-Object Alias          type                            Get-Content Alias          where                           Where-Object Alias          wjb                             Wait-Job Alias          write                           Write-Output

VMware PowerCLI
See VMware PowerCLI

Unable to Run Script
Permissions