CHULETA-POWERSHELL
CHULETA-POWERSHELL
CHULETA-POWERSHELL
To Execute Script
powershell.exe –noexit &”c:\myscript.ps1”
Functions
Parameters separate by space. Return is
optional.
Variables Arrays function sum ([int]$a,[int]$b)
Must start with $ To initialise {
$a = 32 $a = 1,2,4,8 return $a + $b
Can be typed To query }
[int]$a = 32 $b = $a[3] sum 4 5
Constants
Created without $
Set-Variable –name b –value 3.142 –option constant
Referenced with $
$b
Creating Objects
To create an instance of a com object
New-Object -comobject <ProgID>
$a = New-Object –comobject "wscript.network"
$a.username
Miscellaneous
Line Break `
Passing Command Line Arguments Get-Process | Select-Object `
Passed to script with spaces name, ID
myscript.ps1 server1 benp Comments #
Accessed in script by $args array # code here not executed
$servername = $args[0] Merging lines ;
$a=1;$b=3;$c=9
$username = $args[1]
Pipe the output to another command |
40
Get-Service | Get-Member
Do While Loop Do Until Loop
Can repeat a set of commands while a condition is met Can repeat a set of commands until a condition is met
$a=1 $a=1
Do {$a; $a++} Do {$a; $a++}
While ($a –lt 10) Until ($a –gt 10)
41