PowerShell Examples
PowerShell Examples
Eigil Obrestad
and Erik
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
PowerShell Tutorial
Input
System commands
Conditions
if/else
Operators
Eigil Obrestad and Erik Hjelmås
Switch/case
Where
Iteration
For
While
Foreach
August 18, 2015
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
Conditions
if/else
Operators
Switch/case
Where
Iteration
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
(OUSTERHOUT, J., “Scripting: Higher-Level Programming for the 21st Century”,
Credits
IEEE Computer, Vol. 31, No. 3, March 1998, pp. 23-30.)
PowerShell
Eigil Obrestad
and Erik
WARNING!
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
The following presentation is NOT meant to be a
Input
System commands
comprehensive/complete tour of the PowerShell language.
Conditions
if/else
The purpose is to get you started with some basic program
Operators
Switch/case
constructions which you will recognize based on
Where
some-sort-of-programming-background.
Iteration
For
While
At the end of the presentation (Credits section) you will find
Foreach
pointers to more comprehensive material (reference
Math
material).
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Practice
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args You need a Windows host running on a physical or virtual
Input machine with working access to the internet, and with
PowerShell v2.0 installed.
Input
System commands
Conditions
if/else Log in and open a terminal window, download the examples
Operators
Switch/case as we go along from
Where
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Hello World
Hjelmås
Variables
Arrays # hello . ps1
Structures/Classes
Command-line args
Input
Write-Host " hello world ! "
Input
System commands
Iteration
For
or direct from command line cmd (DOSPROMPT)
While
Foreach powershell - command " Write-Host \ " hello world !\ " "
Math
Credits
PowerShell
Eigil Obrestad
and Erik
Single Variables
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Conditions
$firstname = " Mysil "
if/else $lastname = " Bergsprekken "
Operators
Switch/case
$fullname = " $firstname $lastname "
Where Write-Host " Hello $fullname , may I call you " `
Iteration " $firstname `? "
For
While
Foreach
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Exercise
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
$name = " Mysil "
System commands
Conditions
if/else
Operators
Switch/case • Use the properties and methods of this object to
Where
Iteration
⇒ find out how many characters the string contains
For
While
⇒ print the string in upper case
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Single and Double Quotes
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
# quotes . ps1
Conditions
if/else $name = " Mysil "
Operators
Switch/case
Write-Host Hello $name
Where Write-Host " Hello $name "
Iteration Write-Host ' Hello $name '
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Arrays
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
One-dimensional arrays:
Input
Input
System commands
# array . ps1
Conditions
if/else $os = @ ( " linux " , " windows " )
Operators
Switch/case
$os += @ ( " mac " )
Where Write-Host $os [1] # print windows
Iteration Write-Host $os # print array values
For
While
Write-Host $os . Count # length of array
Foreach
Math
Functions
Arrays are created with @(...)
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Associative Arrays
Hjelmås
Variables
Arrays
Structures/Classes
# assoc-array . ps1
Command-line args
Input $user = @ {
Input
System commands
" frodeh " = " Frode Haug " ;
" ivarm " = " Ivar Moe "
Conditions
if/else }
Operators
Switch/case
$user += @ { " lailas " = " Laila Skiaker " }
Where Write-Host $user [ " ivarm " ] # print Ivar Moe
Iteration Write-Host @user # print array values
For
While
Write-Host $user . Keys # print array keys
Foreach Write-Host $user . Count # print length of array
Math
Functions
RegExp
Associative arrays are created with @{...} and are called
PowerShell example Hashtables in PowerShell.
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Structures/Classes
Hjelmås
Credits
PowerShell
Eigil Obrestad
and Erik
Command-Line Arguments
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Conditions
if/else
Operators # cli-args . ps1
Switch/case
Where
Write-Host " I am " $MyInvocation . InvocationName `
Iteration
For " and have " $args . Count " arguments " `
While
Foreach
" first is " $args [0]
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Exercise
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
Conditions
if/else
• Rewrite the previous script to only have one string (just
Operators
Switch/case
one set of double quotes (")), one at the beginning and
Where
one at the end, do not use single quotes either
Iteration
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Input From User
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
# input-user . ps1
Conditions
if/else
Operators $something = Read-Host " Say something here "
Write-Host " you said " $something
Switch/case
Where
Iteration
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Input From the Pipeline
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
# input-pipe . ps1
System commands
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Input From Files
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
# input-file . ps1
Conditions
if/else
Operators $file = Get-Content hello . ps1
Write-Host @file - Separator " `n "
Switch/case
Where
Iteration
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Input from System Commands
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input # input-commands . ps1
System commands
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
if/else
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input # if . ps1
System commands
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Comparison
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input Operator Meaning
System commands
−lt Less than
Conditions
if/else −gt Greater than
Operators
Switch/case −le Less than or equal to
Where
Iteration
−ge Greater than or equal to
For
While
−eq Equal to
Foreach −ne Not equal to
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Boolean
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
Operator Meaning
Conditions
if/else
−not Not
Operators
Switch/case
! Not
Where
−and And
Iteration
For −or Or
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik # if-num-string . ps1
Hjelmås
if ( $args . Count - ne 2) {
Variables
Arrays Write-Host " usage : " `
Structures/Classes
Command-line args
$MyInvocation . InvocationName `
" < argument > < argument > "
Input
Input exit 0
System commands
} elseif ( $args [0] - gt $args [1]) {
Conditions Write-Host $args [0] " larger than " $args [1]
} else {
if/else
Operators
Switch/case
Where
Write-Host $args [0] " smaller than or " `
" equal to " $args [1]
Iteration
For }
While
Foreach
if ( Test-Path $args [0]) {
if (!( Get-Item $args [0]). PSIsContainer ) {
Math
Write-Host $args [0] " is a file "
Functions
}
RegExp
PowerShell example
}
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Boolean example
Hjelmås
Variables
Arrays
# if-bool . ps1
Structures/Classes
Input
Input
Write-Host " And has precedence "
System commands } else {
Conditions Write-Host " Or has precedence "
if/else
Operators
}
Switch/case
Where
# force OR precedence :
Iteration
For
While if ((1 - eq 2) - and ((1 - eq 1) - or (1 - eq 1))) {
Write-Host " And has precedence "
Foreach
Math
} else {
Functions Write-Host " Or has precedence "
RegExp }
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Switch/Case
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args # switch . ps1
Input
Input
System commands
$short = @ { yes = " y " ; nope = " n " }
Conditions
$ans = Read-Host
if/else switch ( $ans ) {
Operators
Switch/case
yes { Write-Host " yes " }
Where nope { Write-Host " nope " ; break }
Iteration { $short . ContainsKey ( " $ans " )} `
For
While
{ Write-Host $short [ $ans ] }
Foreach default { Write-Host " $ans `??? " }
Math }
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Where/Where-Object
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
Iteration
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Exercise
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
• Use Get-Process and Where-Object to
Conditions
if/else ⇒ list all powershell processes
Operators
Switch/case ⇒ store the process table in an array $procs
Where
Iteration
⇒ list all processes with a working set greater than
For
While
10MB
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
For loop
Hjelmås
Credits
PowerShell
Eigil Obrestad
and Erik
While
Hjelmås
# while . ps1
Variables
Arrays while ( $i - le 3) {
Write-Host $i
Structures/Classes
Command-line args
Input $i ++
Input }
System commands
Conditions
if/else
# something more useful :
Operators
$file = Get-ChildItem
Switch/case
Where
Iteration $i =0
For while ( $i - lt $file . Count ) {
if (!( Get-Item $file [ $i ]). PSIsContainer ) {
While
Foreach
Eigil Obrestad
and Erik
Foreach loop
Hjelmås
Credits
PowerShell
Eigil Obrestad
and Erik
ForEach
Hjelmås If we want to read from the pipeline and do stuff object by
Variables
object:
Arrays
Structures/Classes # foreach-pipe . ps1
Command-line args
Eigil Obrestad
and Erik
Operators
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Operator Meaning
Input
Input + Add
System commands
− Subtract
Conditions
if/else
* Multiply
Operators
Switch/case / Divide
Where
% Modulus
Iteration
For
While
Foreach # math . ps1
Math
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Functions
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Conditions # declare :
if/else function add ( $a , $b ) {
Operators
Switch/case Write-Host " $a + $b is " ( $a + $b )
Where
}
Iteration # use :
For
While add 5.12 2.56
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Regular expressions intro 1/5
Hjelmås
Variables
Arrays
Structures/Classes
Special/Meta-characters:
\ | ( ) [ ] { } ˆ $ * + ? .
Command-line args
Input
Input
System commands These have to be protected with \, e.g.
Conditions
if/else
http://www\.hig\.no
Operators
Switch/case
Where To match c:\temp, you need to use the regex
Iteration c:\\temp. As a string in C++ source code, this
For
While regex becomes "c:\\\\temp". Four backslashes
Foreach
to match a single one indeed.
Math
Functions
Credits
PowerShell
Eigil Obrestad
and Erik
Regular expressions intro 2/5
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Conditions
if/else
Operator Meaning
Operators
Switch/case
. Any single character
Where
[abcd] One of these characters
Iteration
For
[ˆabcd] Any one but these characters
While
Foreach
[a-zA-Z0-9] A character in these ranges
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Regular expressions intro 3/5
Hjelmås
Variables
Arrays
Structures/Classes
Grouping:
Command-line args
Input
Input Operator Meaning
()
System commands
Conditions
Group
if/else | OR
Operators
Switch/case
Where
Iteration Anchoring:
For
While
Foreach
Operator Meaning
Math
ˆ Beginning of line
Functions
RegExp
$ End of line
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Regular expressions intro 4/5
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Repetition operators/Modifiers/Quantifiers:
Input
Input
System commands
Operator Meaning
Conditions
if/else ? 0 or 1 time
*
Operators
Switch/case 0 or more times
+
Where
1 or more times
Iteration
For {N} N times
{N,}
While
Foreach At least N times
Math {N,M} At least N but not more than M
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Regular expressions intro 5/5
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
Conditions
Finding URLs in HTML:
if/else (mailto|http)://[ˆ"]*
Operators
Switch/case
Where Each line should be an email address:
Iteration ˆ[A-Za-z0-9._-]+@[A-Za-z0-9.-]+$
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
PowerShell example
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args # regexp . ps1
Input
Input
System commands
$input | ForEach-Object {
Conditions
if ( $_ - match
if/else " ˆ[ A-Za-z0-9 . _- ]+ @ ([ A-Za-z0-9 .-]+) $ " ) {
Operators
Switch/case
Write-Host " Valid email " , $matches [0]
Where Write-Host " Domain is " , $matches [1]
Iteration } else {
For
While
Write-Host " Invalid email address ! "
Foreach }
Math }
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Advanced stuff
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
Input
Input
System commands
Iteration
for much more of what you can do with PowerShell
For
While
Foreach
Math
Functions
RegExp
PowerShell example
PowerShell
only
Credits
PowerShell
Eigil Obrestad
and Erik
Credits
Hjelmås
Variables
Arrays
Structures/Classes
Command-line args
http://refcardz.dzone.com/refcardz/windows-powershell
Input http://powershell.com/cs/blogs/ebook/
Input http://technet.microsoft.com/en-us/library/ee692948.aspx
System commands
http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_
Conditions Quoting_and_Escape_Sequences
if/else
Operators
http://dmitrysotnikov.wordpress.com/2008/11/26/input-gotchas/
Switch/case http://stackoverflow.com/questions/59819/
Where
how-do-i-create-a-custom-type-in-powershell-for-my-scripts-to-use
Iteration http://www.powershellpro.com/powershell-tutorial-introduction/
For
http://en.wikipedia.org/wiki/Windows_PowerShell
While
Foreach http://www.johndcook.com/powershell.html
http://www.regular-expressions.info/
Math
OUSTERHOUT, J., “Scripting: Higher-Level Programming for the 21st Century”,
Functions IEEE Computer, Vol. 31, No. 3, March 1998, pp. 23-30.)
RegExp
PowerShell example
PowerShell
only
Credits