Power Shell Notes For Professionals
Power Shell Notes For Professionals
PowerShell
Notes for Professionals
™
100+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial PowerShell™ group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with PowerShell .................................................................................................... 2
Section 1.1: Allow scripts stored on your machine to run un-signed ........................................................................ 2
Section 1.2: Aliases & Similar Functions ....................................................................................................................... 2
Section 1.3: The Pipeline - Using Output from a PowerShell cmdlet ........................................................................ 3
Section 1.4: Calling .Net Library Methods .................................................................................................................... 4
Section 1.5: Installation or Setup .................................................................................................................................. 5
Section 1.6: Commenting ............................................................................................................................................... 5
Section 1.7: Creating Objects ........................................................................................................................................ 6
Chapter 2: Variables in PowerShell ..................................................................................................................... 7
Section 2.1: Simple variable .......................................................................................................................................... 7
Section 2.2: Arrays ........................................................................................................................................................ 7
Section 2.3: List Assignment of Multiple Variables ..................................................................................................... 7
Section 2.4: Scope ......................................................................................................................................................... 8
Section 2.5: Removing a variable ................................................................................................................................ 8
Chapter 3: Operators .................................................................................................................................................. 9
Section 3.1: Comparison Operators ............................................................................................................................. 9
Section 3.2: Arithmetic Operators ................................................................................................................................ 9
Section 3.3: Assignment Operators ........................................................................................................................... 10
Section 3.4: Redirection Operators ............................................................................................................................ 10
Section 3.5: Mixing operand types, the type of the left operand dictates the behavior ...................................... 11
Section 3.6: Logical Operators ................................................................................................................................... 11
Section 3.7: String Manipulation Operators .............................................................................................................. 11
Chapter 4: Special Operators .............................................................................................................................. 13
Section 4.1: Array Expression Operator ..................................................................................................................... 13
Section 4.2: Call Operation ......................................................................................................................................... 13
Section 4.3: Dot sourcing operator ............................................................................................................................ 13
Chapter 5: Basic Set Operations ......................................................................................................................... 14
Section 5.1: Filtering: Where-Object / where / ? ...................................................................................................... 14
Section 5.2: Ordering: Sort-Object / sort .................................................................................................................. 14
Section 5.3: Grouping: Group-Object / group .......................................................................................................... 15
Section 5.4: Projecting: Select-Object / select .......................................................................................................... 16
Chapter 6: Conditional logic .................................................................................................................................. 17
Section 6.1: if, else and else if ..................................................................................................................................... 17
Section 6.2: Negation .................................................................................................................................................. 17
Section 6.3: If conditional shorthand ......................................................................................................................... 18
Chapter 7: Loops ......................................................................................................................................................... 19
Section 7.1: Foreach ..................................................................................................................................................... 19
Section 7.2: For ............................................................................................................................................................ 19
Section 7.3: ForEach() Method ................................................................................................................................... 19
Section 7.4: ForEach-Object ....................................................................................................................................... 20
Section 7.5: Continue ................................................................................................................................................... 21
Section 7.6: Break ........................................................................................................................................................ 21
Section 7.7: While ......................................................................................................................................................... 22
Section 7.8: Do ............................................................................................................................................................. 22
Chapter 8: Switch statement ................................................................................................................................ 24
Section 8.1: Simple Switch ........................................................................................................................................... 24
Section 8.2: Switch Statement with CaseSensitive Parameter ............................................................................... 24
Section 8.3: Switch Statement with Wildcard Parameter ........................................................................................ 24
Section 8.4: Switch Statement with File Parameter ................................................................................................. 25
Section 8.5: Simple Switch with Default Condition ................................................................................................... 25
Section 8.6: Switch Statement with Regex Parameter ............................................................................................ 26
Section 8.7: Simple Switch With Break ...................................................................................................................... 26
Section 8.8: Switch Statement with Exact Parameter .............................................................................................. 27
Section 8.9: Switch Statement with Expressions ...................................................................................................... 27
Chapter 9: Strings ....................................................................................................................................................... 28
Section 9.1: Multiline string .......................................................................................................................................... 28
Section 9.2: Here-string .............................................................................................................................................. 28
Section 9.3: Concatenating strings ............................................................................................................................ 28
Section 9.4: Special characters .................................................................................................................................. 29
Section 9.5: Creating a basic string ........................................................................................................................... 29
Section 9.6: Format string .......................................................................................................................................... 30
Chapter 10: HashTables ........................................................................................................................................... 31
Section 10.1: Access a hash table value by key ........................................................................................................ 31
Section 10.2: Creating a Hash Table .......................................................................................................................... 31
Section 10.3: Add a key value pair to an existing hash table .................................................................................. 31
Section 10.4: Remove a key value pair from an existing hash table ..................................................................... 32
Section 10.5: Enumerating through keys and Key-Value Pairs .............................................................................. 32
Section 10.6: Looping over a hash table ................................................................................................................... 32
Chapter 11: Working with Objects ....................................................................................................................... 34
Section 11.1: Examining an object ............................................................................................................................... 34
Section 11.2: Updating Objects ................................................................................................................................... 35
Section 11.3: Creating a new object ............................................................................................................................ 35
Section 11.4: Creating Instances of Generic Classes ................................................................................................ 37
Chapter 12: PowerShell Functions ...................................................................................................................... 39
Section 12.1: Basic Parameters ................................................................................................................................... 39
Section 12.2: Advanced Function ............................................................................................................................... 39
Section 12.3: Mandatory Parameters ........................................................................................................................ 41
Section 12.4: Parameter Validation ............................................................................................................................ 41
Section 12.5: Simple Function with No Parameters .................................................................................................. 43
Chapter 13: PowerShell Classes ........................................................................................................................... 44
Section 13.1: Listing available constructors for a class ............................................................................................ 44
Section 13.2: Methods and properties ....................................................................................................................... 45
Section 13.3: Constructor overloading ....................................................................................................................... 45
Section 13.4: Get All Members of an Instance ........................................................................................................... 46
Section 13.5: Basic Class Template ............................................................................................................................ 46
Section 13.6: Inheritance from Parent Class to Child Class ..................................................................................... 47
Chapter 14: PowerShell Modules ........................................................................................................................ 48
Section 14.1: Create a Module Manifest ..................................................................................................................... 48
Section 14.2: Simple Module Example ....................................................................................................................... 48
Section 14.3: Exporting a Variable from a Module ................................................................................................... 49
Section 14.4: Structuring PowerShell Modules .......................................................................................................... 49
Section 14.5: Location of Modules .............................................................................................................................. 49
Section 14.6: Module Member Visibility ..................................................................................................................... 49
Chapter 15: PowerShell profiles .......................................................................................................................... 50
Section 15.1: Create an basic profile .......................................................................................................................... 50
Chapter 16: Calculated Properties ..................................................................................................................... 51
Section 16.1: Display file size in KB - Calculated Properties ..................................................................................... 51
Chapter 17: Using existing static classes ........................................................................................................ 52
Section 17.1: Adding types ........................................................................................................................................... 52
Section 17.2: Using the .Net Math Class ..................................................................................................................... 52
Section 17.3: Creating new GUID instantly ................................................................................................................ 52
Chapter 18: Built-in variables ................................................................................................................................ 54
Section 18.1: $PSScriptRoot ......................................................................................................................................... 54
Section 18.2: $Args ...................................................................................................................................................... 54
Section 18.3: $PSItem ................................................................................................................................................... 54
Section 18.4: $? ............................................................................................................................................................. 54
Section 18.5: $error ...................................................................................................................................................... 54
Chapter 19: Automatic Variables ........................................................................................................................ 56
Section 19.1: $OFS ........................................................................................................................................................ 56
Section 19.2: $? ............................................................................................................................................................. 56
Section 19.3: $null ......................................................................................................................................................... 56
Section 19.4: $error ...................................................................................................................................................... 57
Section 19.5: $pid ......................................................................................................................................................... 57
Section 19.6: Boolean values ...................................................................................................................................... 57
Section 19.7: $_ / $PSItem .......................................................................................................................................... 58
Section 19.8: $PSVersionTable ................................................................................................................................... 58
Chapter 20: Environment Variables .................................................................................................................. 59
Section 20.1: Windows environment variables are visible as a PS drive called Env: ............................................ 59
Section 20.2: Instant call of Environment Variables with $env: .............................................................................. 59
Chapter 21: Splatting ................................................................................................................................................ 60
Section 21.1: Piping and Splatting ............................................................................................................................... 60
Section 21.2: Passing a Switch parameter using Splatting ...................................................................................... 60
Section 21.3: Splatting From Top Level Function to a Series of Inner Function .................................................... 61
Section 21.4: Splatting parameters ............................................................................................................................ 61
Chapter 22: PowerShell "Streams"; Debug, Verbose, Warning, Error, Output and
Information .................................................................................................................................................................... 63
Section 22.1: Write-Output .......................................................................................................................................... 63
Section 22.2: Write Preferences ................................................................................................................................. 63
Chapter 23: Sending Email ..................................................................................................................................... 65
Section 23.1: Send-MailMessage with predefined parameters ............................................................................... 65
Section 23.2: Simple Send-MailMessage ................................................................................................................... 66
Section 23.3: SMTPClient - Mail with .txt file in body message ............................................................................... 66
Chapter 24: PowerShell Remoting .................................................................................................................... 67
Section 24.1: Connecting to a Remote Server via PowerShell ................................................................................ 67
Section 24.2: Run commands on a Remote Computer ........................................................................................... 67
Section 24.3: Enabling PowerShell Remoting ........................................................................................................... 69
Section 24.4: A best practise for automatically cleaning-up PSSessions .............................................................. 70
Chapter 25: Working with the PowerShell pipeline ................................................................................... 71
Section 25.1: Writing Functions with Advanced Lifecycle ........................................................................................ 71
Section 25.2: Basic Pipeline Support in Functions .................................................................................................... 71
Section 25.3: Working concept of pipeline ............................................................................................................... 72
Chapter 26: PowerShell Background Jobs .................................................................................................... 73
Section 26.1: Basic job creation .................................................................................................................................. 73
Section 26.2: Basic job management ........................................................................................................................ 73
Chapter 27: Return behavior in PowerShell .................................................................................................. 75
Section 27.1: Early exit ................................................................................................................................................. 75
Section 27.2: Gotcha! Return in the pipeline ............................................................................................................. 75
Section 27.3: Return with a value ............................................................................................................................... 75
Section 27.4: How to work with functions returns .................................................................................................... 75
Section 27.5: Gotcha! Ignoring unwanted output ..................................................................................................... 77
Chapter 28: CSV parsing ......................................................................................................................................... 78
Section 28.1: Basic usage of Import-Csv ................................................................................................................... 78
Section 28.2: Import from CSV and cast properties to correct type ..................................................................... 78
Chapter 29: Working with XML Files .................................................................................................................. 80
Section 29.1: Accessing an XML File ........................................................................................................................... 80
Section 29.2: Creating an XML Document using XmlWriter() ................................................................................. 81
Section 29.3: Adding snippets of XML to current XMLDocument ........................................................................... 82
Chapter 30: Communicating with RESTful APIs ........................................................................................... 88
Section 30.1: Post Message to hipChat ...................................................................................................................... 88
Section 30.2: Using REST with PowerShell Objects to GET and POST many items .............................................. 88
Section 30.3: Use Slack.com Incoming Webhooks .................................................................................................. 88
Section 30.4: Using REST with PowerShell Objects to Get and Put individual data ............................................. 89
Section 30.5: Using REST with PowerShell to Delete items ..................................................................................... 89
Chapter 31: PowerShell SQL queries ................................................................................................................. 90
Section 31.1: SQLExample ............................................................................................................................................ 90
Section 31.2: SQLQuery ............................................................................................................................................... 90
Chapter 32: Regular Expressions ........................................................................................................................ 91
Section 32.1: Single match .......................................................................................................................................... 91
Section 32.2: Replace .................................................................................................................................................. 93
Section 32.3: Replace text with dynamic value using a MatchEvalutor ................................................................ 93
Section 32.4: Escape special characters ................................................................................................................... 94
Section 32.5: Multiple matches ................................................................................................................................... 94
Chapter 33: Aliases ..................................................................................................................................................... 97
Section 33.1: Get-Alias ................................................................................................................................................. 97
Section 33.2: Set-Alias ................................................................................................................................................. 97
Chapter 34: Using the progress bar ................................................................................................................. 98
Section 34.1: Simple use of progress bar .................................................................................................................. 98
Section 34.2: Usage of inner progress bar ............................................................................................................... 98
Chapter 35: PowerShell.exe Command-Line ............................................................................................... 100
Section 35.1: Executing a command ........................................................................................................................ 100
Section 35.2: Executing a script file ......................................................................................................................... 101
Chapter 36: Cmdlet Naming ................................................................................................................................ 102
Section 36.1: Verbs ..................................................................................................................................................... 102
Section 36.2: Nouns ................................................................................................................................................... 102
Chapter 37: Running Executables .................................................................................................................... 103
Section 37.1: GUI Applications .................................................................................................................................. 103
Section 37.2: Console Streams ................................................................................................................................. 103
Section 37.3: Exit Codes ............................................................................................................................................ 103
Chapter 38: Enforcing script prerequisites ................................................................................................. 104
Section 38.1: Enforce minimum version of PowerShell host .................................................................................. 104
Section 38.2: Enforce running the script as administrator .................................................................................... 104
Chapter 39: Using the Help System ................................................................................................................. 105
Section 39.1: Updating the Help System ................................................................................................................. 105
Section 39.2: Using Get-Help .................................................................................................................................... 105
Section 39.3: Viewing online version of a help topic .............................................................................................. 105
Section 39.4: Viewing Examples ............................................................................................................................... 105
Section 39.5: Viewing the Full Help Page ................................................................................................................ 106
Section 39.6: Viewing help for a specific parameter ............................................................................................. 106
Chapter 40: Modules, Scripts and Functions ............................................................................................. 107
Section 40.1: Function ............................................................................................................................................... 107
Section 40.2: Script .................................................................................................................................................... 107
Section 40.3: Module ................................................................................................................................................. 108
Section 40.4: Advanced Functions .......................................................................................................................... 109
Chapter 41: Naming Conventions ..................................................................................................................... 112
Section 41.1: Functions ............................................................................................................................................... 112
Chapter 42: Common parameters .................................................................................................................. 113
Section 42.1: ErrorAction parameter ....................................................................................................................... 113
Chapter 43: Parameter sets ............................................................................................................................... 115
Section 43.1: Parameter set to enforce the use of a parameter when a other is selected ............................... 115
Section 43.2: Parameter set to limit the combination of parameters ................................................................. 115
Chapter 44: PowerShell Dynamic Parameters ......................................................................................... 116
Section 44.1: "Simple" dynamic parameter ............................................................................................................ 116
Chapter 45: GUI in PowerShell ........................................................................................................................... 118
Section 45.1: WPF GUI for Get-Service cmdlet ....................................................................................................... 118
Chapter 46: URL Encode/Decode .................................................................................................................... 120
Section 46.1: Encode Query String with `[System.Web.HttpUtility]::UrlEncode()` ............................................... 120
Section 46.2: Quick Start: Encoding ......................................................................................................................... 120
Section 46.3: Quick Start: Decoding ........................................................................................................................ 120
Section 46.4: Encode Query String with `[uri]::EscapeDataString()` .................................................................... 121
Section 46.5: Decode URL with `[uri]::UnescapeDataString()` .............................................................................. 121
Section 46.6: Decode URL with `[System.Web.HttpUtility]::UrlDecode()` ............................................................ 123
Chapter 47: Error handling ................................................................................................................................. 126
Section 47.1: Error Types .......................................................................................................................................... 126
Chapter 48: Package management ............................................................................................................... 128
Section 48.1: Create the default PowerShell Module Repository .......................................................................... 128
Section 48.2: Find a module by name .................................................................................................................... 128
Section 48.3: Install a Module by name .................................................................................................................. 128
Section 48.4: Uninstall a module my name and version ....................................................................................... 128
Section 48.5: Update a module by name ............................................................................................................... 128
Section 48.6: Find a PowerShell module using a pattern ...................................................................................... 128
Chapter 49: TCP Communication with PowerShell .................................................................................. 129
Section 49.1: TCP listener .......................................................................................................................................... 129
Section 49.2: TCP Sender ......................................................................................................................................... 129
Chapter 50: PowerShell Workflows ................................................................................................................. 131
Section 50.1: Workflow with Input Parameters ....................................................................................................... 131
Section 50.2: Simple Workflow Example ................................................................................................................ 131
Section 50.3: Run Workflow as a Background Job ............................................................................................... 131
Section 50.4: Add a Parallel Block to a Workflow ................................................................................................. 131
Chapter 51: Embedding Managed Code (C# | VB) .................................................................................... 133
Section 51.1: C# Example ........................................................................................................................................... 133
Section 51.2: VB.NET Example .................................................................................................................................. 133
Chapter 52: How to download latest artifact from Artifactory using PowerShell script
(v2.0 or below)? ......................................................................................................................................................... 135
Section 52.1: PowerShell Script for downloading the latest artifact .................................................................... 135
Chapter 53: Comment-based help ................................................................................................................... 136
Section 53.1: Function comment-based help .......................................................................................................... 136
Section 53.2: Script comment-based help .............................................................................................................. 138
Chapter 54: Archive Module ............................................................................................................................... 141
Section 54.1: Compress-Archive with wildcard ....................................................................................................... 141
Section 54.2: Update existing ZIP with Compress-Archive ................................................................................... 141
Section 54.3: Extract a Zip with Expand-Archive .................................................................................................... 141
Chapter 55: Infrastructure Automation ........................................................................................................ 142
Section 55.1: Simple script for black-box integration test of console applications ............................................ 142
Chapter 56: PSScriptAnalyzer - PowerShell Script Analyzer .............................................................. 143
Section 56.1: Analyzing scripts with the built-in preset rulesets ........................................................................... 143
Section 56.2: Analyzing scripts against every built-in rule ................................................................................... 143
Section 56.3: List all built-in rules ............................................................................................................................. 143
Chapter 57: Desired State Configuration .................................................................................................... 145
Section 57.1: Simple example - Enabling WindowsFeature ................................................................................... 145
Section 57.2: Starting DSC (mof) on remote machine .......................................................................................... 145
Section 57.3: Importing psd1 (data file) into local variable ................................................................................... 145
Section 57.4: List available DSC Resources ............................................................................................................ 145
Section 57.5: Importing resources for use in DSC .................................................................................................. 146
Chapter 58: Using ShouldProcess .................................................................................................................... 147
Section 58.1: Full Usage Example ............................................................................................................................ 147
Section 58.2: Adding -WhatIf and -Confirm support to your cmdlet .................................................................. 148
Section 58.3: Using ShouldProcess() with one argument ..................................................................................... 148
Chapter 59: Scheduled tasks module ............................................................................................................ 149
Section 59.1: Run PowerShell Script in Scheduled Task ......................................................................................... 149
Chapter 60: ISE module ......................................................................................................................................... 150
Section 60.1: Test Scripts .......................................................................................................................................... 150
Chapter 61: Creating DSC Class-Based Resources .................................................................................. 151
Section 61.1: Create a DSC Resource Skeleton Class ............................................................................................. 151
Section 61.2: DSC Resource Skeleton with Key Property ...................................................................................... 151
Section 61.3: DSC Resource with Mandatory Property .......................................................................................... 151
Section 61.4: DSC Resource with Required Methods ............................................................................................. 152
Chapter 62: WMI and CIM ...................................................................................................................................... 153
Section 62.1: Querying objects ................................................................................................................................. 153
Section 62.2: Classes and namespaces .................................................................................................................. 155
Chapter 63: ActiveDirectory module .............................................................................................................. 158
Section 63.1: Users ..................................................................................................................................................... 158
Section 63.2: Module ................................................................................................................................................. 158
Section 63.3: Groups .................................................................................................................................................. 158
Section 63.4: Computers ........................................................................................................................................... 159
Section 63.5: Objects ................................................................................................................................................. 159
Chapter 64: SharePoint Module ........................................................................................................................ 160
Section 64.1: Loading SharePoint Snap-In .............................................................................................................. 160
Section 64.2: Iterating over all lists of a site collection ......................................................................................... 160
Section 64.3: Get all installed features on a site collection ................................................................................... 160
Chapter 65: Introduction to Psake .................................................................................................................. 161
Section 65.1: Basic outline ......................................................................................................................................... 161
Section 65.2: FormatTaskName example .............................................................................................................. 161
Section 65.3: Run Task conditionally ....................................................................................................................... 161
Section 65.4: ContinueOnError ................................................................................................................................. 162
Chapter 66: Introduction to Pester .................................................................................................................. 163
Section 66.1: Getting Started with Pester ................................................................................................................ 163
Chapter 67: Handling Secrets and Credentials ......................................................................................... 164
Section 67.1: Accessing the Plaintext Password ..................................................................................................... 164
Section 67.2: Prompting for Credentials ................................................................................................................. 164
Section 67.3: Working with Stored Credentials ...................................................................................................... 164
Section 67.4: Storing the credentials in Encrypted form and Passing it as parameter when Required
............................................................................................................................................................................. 165
Chapter 68: Security and Cryptography ...................................................................................................... 166
Section 68.1: Calculating a string's hash codes via .Net Cryptography ............................................................... 166
Chapter 69: Signing Scripts ................................................................................................................................. 167
Section 69.1: Signing a script .................................................................................................................................... 167
Section 69.2: Bypassing execution policy for a single script ................................................................................ 167
Section 69.3: Changing the execution policy using Set-ExecutionPolicy ............................................................. 167
Section 69.4: Get the current execution policy ....................................................................................................... 168
Section 69.5: Getting the signature from a signed script ...................................................................................... 168
Section 69.6: Creating a self-signed code signing certificate for testing ............................................................ 168
Chapter 70: Anonymize IP (v4 and v6) in text file with PowerShell ................................................ 170
Section 70.1: Anonymize IP address in text file ....................................................................................................... 170
Chapter 71: Amazon Web Services (AWS) Rekognition ......................................................................... 171
Section 71.1: Detect Image Labels with AWS Rekognition ..................................................................................... 171
Section 71.2: Compare Facial Similarity with AWS Rekognition ........................................................................... 171
Chapter 72: Amazon Web Services (AWS) Simple Storage Service (S3) ...................................... 173
Section 72.1: Create a new S3 Bucket ...................................................................................................................... 173
Section 72.2: Upload a Local File Into an S3 Bucket ............................................................................................. 173
Section 72.3: Delete a S3 Bucket ............................................................................................................................. 173
Credits ............................................................................................................................................................................ 174
You may also like ...................................................................................................................................................... 176
About
Please feel free to share this PDF with anyone for free,
latest version of this book can be downloaded from:
http://GoalKicker.com/PowerShellBook
This PowerShell™ Notes for Professionals book is compiled from Stack Overflow
Documentation, the content is written by the beautiful people at Stack Overflow.
Text content is released under Creative Commons BY-SA, see credits at the end
of this book whom contributed to the various chapters. Images may be copyright
of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not
affiliated with official PowerShell™ group(s) or company(s) nor Stack Overflow. All
trademarks and registered trademarks are the property of their respective
company owners
Set-ExecutionPolicy RemoteSigned
A temporary workaround for execution policy can also be achieved by running the PowerShell executable and
passing any valid policy as -ExecutionPolicy parameter. The policy is in effect only during process' lifetime, so no
administrative access to the registry is needed.
There are multiple other policies available, and sites online often encourage you to use Set-ExecutionPolicy
Unrestricted. This policy stays in place until changed, and lowers the system security stance. This is not advisable.
Use of RemoteSigned is recommended because it allows locally stored and written code, and requires remotely
acquired code be signed with a certificate from a trusted root.
Also, beware that the Execution Policy may be enforced by Group Policy, so that even if the policy is changed to
Unrestricted system-wide, Group Policy may revert that setting at its next enforcement interval (typically 15
minutes). You can see the execution policy set at the various scopes using Get-ExecutionPolicy -List
TechNet Documentation:
Set-ExecutionPolicy
about_Execution_Policies
Using Write-Host:
Using Write-Output:
It's worth noting that although Write-Output & Write-Host both write to the screen there is a subtle difference.
Write-Host writes only to stdout (i.e. the console screen), whereas Write-Output writes to both stdout AND to the
output [success] stream allowing for redirection. Redirection (and streams in general) allow for the output of one
command to be directed as input to another including assignment to a variable.
These similar functions are not aliases, but can produce the same results if one wants to avoid "polluting" the
success stream.
'Hello world'
Hello world
Another example of aliases in PowerShell is the common mapping of both older command prompt commands and
BASH commands to PowerShell cmdlets. All of the following produce a directory listing of the current directory.
C:\Windows> dir
C:\Windows> ls
C:\Windows> Get-ChildItem
Finally, you can create your own alias with the Set-Alias cmdlet! As an example let's alisas Test-NetConnection,
which is essentially the PowerShell equivalent to the command prompt's ping command, to "ping".
Now you can use ping instead of Test-NetConnection! Be aware that if the alias is already in use, you'll overwrite
the association.
The Alias will be alive, till the session is active. Once you close the session and try to run the alias which you have
created in your last session, it will not work. To overcome this issue, you can import all your aliases from an excel
into your session once, before starting your work.
The pipeline symbol | is used at the end of a cmdlet to take the data it exports and feed it to the next cmdlet. A
simple example is using Select-Object to only show the Name property of a file shown from Get-ChildItem:
More advanced usage of the pipeline allows us to pipe the output of a cmdlet into a foreach loop:
Get-ChildItem | ForEach-Object {
Copy-Item -Path $_.FullName -destination C:\NewDirectory\
}
Note that the example above uses the $_ automatic variable. $_ is the short alias of $PSItem which is an automatic
variable which contains the current item in the pipeline.
#calling Path.GetFileName()
C:\> [System.IO.Path]::GetFileName('C:\Windows\explorer.exe')
explorer.exe
Static methods can be called from the class itself, but calling non-static methods requires an instance of the .Net
class (an object).
For example, the AddHours method cannot be called from the System.DateTime class itself. It requires an instance
of the class:
C:\> [System.DateTime]::AddHours(15)
Method invocation failed because [System.DateTime] does not contain a method named 'AddHours'.
At line:1 char:1
+ [System.DateTime]::AddHours(15)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Then, we can use methods of that object, even methods which cannot be called directly from the System.DateTime
class, like the AddHours method:
C:\> $Object.AddHours(15)
PowerShell is included with the Windows Management Framework. Installation and Setup are not required on
modern versions of Windows.
Updates to PowerShell can be accomplished by installing a newer version of the Windows Management Framework.
Other Platforms
PowerShell 6 can be installed on other platforms. The installation packages are available here.
For example, PowerShell 6, for Ubuntu 16.04, is published to package repositories for easy installation (and
updates).
# Update apt-get
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
powershell
After registering the Microsoft repository once as superuser, from then on, you just need to use sudo apt-get
upgrade powershell to update it. Then just run powershell
You can also have multi-line comments using <# and #> at the beginning and end of the comment respectively.
<#
This is a
multi-line
comment
#>
Get-ChildItem
In many instances, a new object will be created in order to export data or pass it to another commandlet. This can
be done like so:
There are many ways of creating an object. The following method is probably the shortest and fastest way to create
a PSCustomObject:
$newObject = [PSCustomObject]@{
ComputerName = 'SERVER1'
Role = 'Interface'
Environment = 'Production'
}
In case you already have an object, but you only need one or two extra properties, you can simply add that
property by using Select-Object:
All objects can be stored in variables or passed into the pipeline. You could also add these objects to a collection
and then show the results at the end.
Collections of objects work well with Export-CSV (and Import-CSV). Each line of the CSV is an object, each column a
property.
Format commands convert objects into text stream for display. Avoid using Format-* commands until the final step
of any data processing, to maintain the usability of the objects.
$foo = "bar"
This statement allocates a variable called foo with a string value of "bar".
$myArrayOfInts = 1,2,3,4
$myArrayOfStrings = "1","2","3","4"
Adding to an array
$myArrayOfInts = $myArrayOfInts + 5
# now contains 1,2,3,4 & 5!
$myArrayOfInts = 1,2,3,4
$myOtherArrayOfInts = 5,6,7
$myArrayOfInts = $myArrayOfInts + $myOtherArrayOfInts
# now 1,2,3,4,5,6,7
$input = "foo.bar.baz"
$parts = $input.Split(".")
$foo = $parts[0]
$bar = $parts[1]
$baz = $parts[2]
Since Powershell treats assignments in this manner like lists, if there are more values in the list than items in your
Output:
Global Scope Function (local) scope Function (local) scope Global Scope Global Scope
Remove-Item Variable:\foo
Variable has a provider to allow most *-item cmdlets to work much like file systems.
Another method to remove variable is to use Remove-Variable cmdlet and its alias rv
$var = "Some Variable" #Define variable 'var' containing the string 'Some Variable'
$var #For test and show string 'Some Variable' on the console
Names can be preceded by special characters to modify the behavior of the operator:
Case-Insensitive is the default if not specified, ("a" -eq "A") same as ("a" -ieq "A").
"abc", "def" -contains "def" # Returns true when the value (right) is present
# in the array (left)
"abc", "def" -notcontains "123" # Returns true when the value (right) is not present
# in the array (left)
"def" -in "abc", "def" # Returns true when the value (left) is present
# in the array (right)
"123" -notin "abc", "def" # Returns true when the value (left) is not present
# in the array (right)
cmdlet > file # Send success output to file, overwriting existing content
cmdlet >> file # Send success output to file, appending to existing content
cmdlet 1>&2 # Send success and error output to error stream
cmdlet 2> file # Send error output to file, overwriting existing content
cmdlet 2>> file # Send error output to file, appending to existing content
cmdlet 2>&1 # Send success and error output to success output stream
cmdlet 3> file # Send warning output to file, overwriting existing content
cmdlet 3>> file # Send warning output to file, appending to existing content
cmdlet 3>&1 # Send success and warning output to success output stream
cmdlet 4> file # Send verbose output to file, overwriting existing content
cmdlet 4>> file # Send verbose output to file, appending to existing content
cmdlet 4>&1 # Send success and verbose output to success output stream
cmdlet 5> file # Send debug output to file, overwriting existing content
cmdlet 5>> file # Send debug output to file, appending to existing content
cmdlet 5>&1 # Send success and debug output to success output stream
cmdlet 6> file # Send information output to file, overwriting existing content
cmdlet 6>> file # Send information output to file, appending to existing content
cmdlet *> file # Send all output streams to file, overwriting existing content
cmdlet *>> file # Send all output streams to file, appending to existing content
cmdlet *>&1 # Send all output streams to success output stream
Redirection operators only redirect streams to files or streams to streams. The pipe operator pumps an object
down the pipeline to a cmdlet or the output. How the pipeline works differs in general from how redirection works
and can be read on Working with the PowerShell pipeline
For Multiplication
The -replace operator replaces a pattern in an input value using a regular expression. This operator uses two
arguments (separated by a comma): a regular expression pattern and its replacement value (which is optional and
an empty string by default).
"A B C" -split " " #Returns an array string collection object containing A,B and C.
@(Get-ChildItem $env:windir\System32\ntdll.dll)
@(Get-ChildItem $env:windir\System32)
Will return an array with all the items in the folder (which is not a change of behavior from the inner expression.
runs .\myScript.ps1 in the current scope making any functions, and variable available in the current scope.
Synonyms:
Where-Object
where
?
Example:
Returns:
Aaron
Albert
Alphonse
Synonyms:
Sort-Object
sort
Assuming:
$names | Sort-Object
$names | sort
Aaron
Aaron
Bernie
Danny
Charlie
Bernie
Aaron
Aaron
Aaron
Aaron
Danny
Bernie
Charlie
Synonyms:
Group-Object
group
Examples:
Response:
Synonyms:
Select-Object
SELECT
$d | select -first 1 *
PSPath
PSParentPath
PSChildName
PSDrive
PSProvider
PSIsContainer
BaseName
Mode
Name
Parent
Exists
Root
FullName
Extension
CreationTime
CreationTimeUtc
LastAccessTime
LastAccessTimeUtc
LastWriteTime
LastWriteTimeUtc
Attributes
With an if the commands inside the brackets ({}) are only executed if the conditions inside the if(()) are met
$test = "test"
if ($test -eq "test"){
Write-Host "if condition met"
}
You can also do an else. Here the else commands are executed if the if conditions are not met:
$test = "test"
if ($test -eq "test2"){
Write-Host "if condition met"
}
else{
Write-Host "if condition not met"
}
or an elseif. An else if runs the commands if the if conditions are not met and the elseif conditions are met:
$test = "test"
if ($test -eq "test2"){
Write-Host "if condition met"
}
elseif ($test -eq "test"){
Write-Host "ifelse condition met"
}
Note the above use -eq(equality) CmdLet and not = or == as many other languages do for equality.
$test = "test"
if (-Not $test -eq "test2"){
Write-Host "if condition not met"
}
$test = "test"
if (!($test -eq "test2")){
Write-Host "if condition not met"
}
If($boolean){
# this does not run because $boolean is false
Write-Host "Shorthand If conditions can be nice, just make sure they are always boolean."
}
If($string){
# This does run because the string is non-zero length
Write-Host "If the variable is not strictly null or Boolean false, it will evaluate to true as
it is an object or string with length greater than 0."
}
If($emptyString){
# This does not run because the string is zero-length
Write-Host "Checking empty strings can be useful as well."
}
If($null){
# This does not run because the condition is null
Write-Host "Checking Nulls will not print this statement."
}
This example demonstrates printing all items in an array to the console host:
Like the last example, this example, instead, demonstrates creating an array prior to storing the loop:
$Numbers = @()
ForEach ($Number in 1..20)
{
$Numbers += $Number
}
A typical use of the for loop is to operate on a subset of the values in an array. In most cases, if you want to iterate
all values in an array, consider using a foreach statement.
Instead of the ForEach-Object cmdlet, the here is also the possibility to use a ForEach method directly on object
arrays like so
(1..10).ForEach({$_ * $_})
(1..10).ForEach{$_ * $_}
1
4
9
16
25
36
49
64
81
100
Basic usage
$object | ForEach-Object {
code_block
}
Example:
$names = @("Any","Bob","Celine","David")
$names | ForEach-Object {
"Hi, my name is $_!"
}
Foreach-Object has two default aliases, foreach and % (shorthand syntax). Most common is % because foreach can
be confused with the foreach statement. Examples:
$names | % {
"Hi, my name is $_!"
}
$names | foreach {
"Hi, my name is $_!"
}
Advanced usage
Foreach-Object stands out from the alternative foreach solutions because it's a cmdlet which means it's designed
to use the pipeline. Because of this, it has support for three scriptblocks just like a cmdlet or advanced function:
Begin: Executed once before looping through the items that arrive from the pipeline. Usually used to create
functions for use in the loop, creating variables, opening connections (database, web +) etc.
Process: Executed once per item arrived from the pipeline. "Normal" foreach codeblock. This is the default
used in the examples above when the parameter isn't specified.
End: Executed once after processing all items. Usually used to close connections, generate a report etc.
Example:
$i =0
while ($i -lt 20) {
$i++
if ($i -eq 7) { continue }
Write-Host $I
}
The above will output 1 to 20 to the console but miss out the number 7.
Note: When using a pipeline loop you should use return instead of Continue.
$i = 0
while ($i -lt 15) {
$i++
if ($i -eq 7) {break}
Write-Host $i
}
Note: When using a pipeline loop, break will behave as continue. To simulate break in the pipeline loop you need
to incorporate some additional logic, cmdlet, etc. It is easier to stick with non-pipeline loops if you need to use
break.
Break Labels
Break can also call a label that was placed in front of the instantiation of a loop:
$i = 0
:mainLoop While ($i -lt 15) {
Write-Host $i -ForegroundColor 'Cyan'
$j = 0
While ($j -lt 15) {
Write-Host $j -ForegroundColor 'Magenta'
$k = $i*$j
Write-Host $k -ForegroundColor 'Green'
Note: This code will increment $i to 8 and $j to 13 which will cause $k to equal 104. Since $k exceed 100, the code
will then break out of both loops.
while(condition){
code_block
}
The following example creates a loop that will count down from 10 to 0
$i = 10
while($i -ge 0){
$i
$i--
}
Unlike the Do-While loop the condition is evaluated prior to the action's first execution. The action will not be
performed if the initial condition evaluates to false.
Note: When evaluating the condition, PowerShell will treat the existence of a return object as true. This can be used
in several ways but below is an example to monitor for a process. This example will spawn a notepad process and
then sleep the current shell as long as that process is running. When you manually close the notepad instance the
while condition will fail and the loop will break.
Start-Process notepad.exe
while(Get-Process notepad -ErrorAction SilentlyContinue){
Start-Sleep -Milliseconds 500
}
Section 7.8: Do
Do-loops are useful when you always want to run a codeblock at least once. A Do-loop will evaluate the condition
after executing the codeblock, unlike a while-loop which does it before executing the codeblock.
Do {
code_block
} while (condition)
Loop until the condition is true, in other words, loop while the condition is false:
Real Examples:
$i = 0
Do {
$i++
"Number $i"
} while ($i -ne 3)
Do {
$i++
"Number $i"
} until ($i -eq 3)
Do-While and Do-Until are antonymous loops. If the code inside the same, the condition will be reversed. The
example above illustrates this behaviour.
switch($myValue)
{
'First Condition' { 'First Action' }
'Second Condition' { 'Second Action' }
}
Example:
Output:
Second Action
The second action is the only action executed because it is the only condition that exactly matches the string
'Condition' when accounting for case-sensitivity.
Example:
Output:
Normal match
Zero or more wildcard chars.
Range and set of chars.
Single char. wildcard
condition
test
Output:
First Action
Second Action
Example:
switch('Condition')
{
'Skip Condition'
{
'First Action'
}
'Skip This Condition Too'
{
'Second Action'
}
Default
{
Output:
Default Action
Example:
Output:
Example:
switch('Condition')
{
'Condition'
{
'First Action'
}
'Condition'
{
'Second Action'
break
}
'Condition'
{
'Third Action'
}
}
Output:
First Action
Second Action
Example:
Output:
First Action
Second Action
Third Action
The first through third actions are executed because their associated conditions matched the input. The regex and
wildcard strings in the fourth and fifth conditions fail matching.
Note that the fourth condition would also match the input string if regular expression matching was being
performed, but was ignored in this case because it is not.
$myInput = 0
switch($myInput) {
# because the result of the expression, 4,
# does not equal our input this block should not be run.
(2+2) { 'True. 2 +2 = 4' }
#Output
True. 2-2 = 0
True. Value is 0
You can use the special characters for carriage return and/or newline manually or use the NewLine-
environment variable to insert the systems "newline" value)
"Hello`r`nWorld"
"Hello{0}World" -f [environment]::NewLine
"Hello
World"
@"
Hello
World
"@
Here-string
Here-strings begin with @" and a linebreak and end with "@ on its own line ("@must be first characters on the line,
not even whitespace/tab).
@"
Simple
Multiline string
with "quotes"
"@
Literal here-string
You could also create a literal here-string by using single quotes, when you don't want any expressions to be
expanded just like a normal literal string.
@'
The following line won't be expanded
$(Get-Date)
because this is a literal here-string
'@
$string1 = "Power"
$string2 = "Shell"
"Greetings from $string1$string2"
Using subexpressions
The output/result of a subexpressions $() can be used in a string. This is useful when accessing properties of an
object or performing a complex expression. Subexpressions can contain multiple statements separated by
semicolon ;
"Tomorrow is $((Get-Date).AddDays(1).DayOfWeek)"
`0 #Null
`a #Alert/Beep
`b #Backspace
`f #Form feed (used for printer output)
`n #New line
`r #Carriage return
`t #Horizontal tab
`v #Vertical tab (used for printer output)
Example:
`# #Comment-operator
`$ #Variable operator
`` #Escape character
`' #Single quote
`" #Double quote
To use a double quote inside a string it needs to be escaped using the escape character, backtick (`). Single quotes
can be used inside a double-quoted string.
$myString = "A `"double quoted`" string which also has 'single quotes'."
Literal string
Literal strings are strings that doesn't evaluate variables and special characters. It's created using single quotes.
To use single quotes inside a literal string, use double single quotes or a literal here-string. Double quotes can be
used safely inside a literal string
Format strings can be used with the -f operator or the static [String]::Format(string format, args) .NET
method.
$hashTable = @{
Key1 = 'Value1'
Key2 = 'Value2'
}
$hashTable.Key1
#output
Value1
$hashTable = @{
'Key 1' = 'Value3'
Key2 = 'Value4'
}
$hashTable.'Key 1'
#Output
Value3
$hashTable = @{}
$hashTable = @{
Name1 = 'Value'
Name2 = 'Value'
Name3 = 'Value3'
}
$hashTable = @{
Key1 = 'Value1'
}
$hashTable += @{Key2 = 'Value2'}
$hashTable
#Output
Name Value
---- -----
Key1 Value1
An example, to add a "Key2" key with a value of "Value2" to the hash table using the Add method:
$hashTable = @{
Key1 = 'Value1'
}
$hashTable.Add("Key2", "Value2")
$hashTable
#Output
Name Value
---- -----
Key1 Value1
Key2 Value2
$hashTable = @{
Key1 = 'Value1'
Key2 = 'Value2'
}
$hashTable.Remove("Key2", "Value2")
$hashTable
#Output
Name Value
---- -----
Key1 Value1
foreach($key in $hashTable.Keys)
{
$value = $hashTable.$key
Write-Output "$key : $value"
}
#Output
Key1 : Value1
Key2 : Value2
This yields:
TypeName: System.IO.DirectoryInfo
Another way to get the type of an object is to use the GetType method, like so:
To view a list of properties the object has, along with their values, you can use the Format-List cmdlet with its
Property parameter set to: * (meaning all).
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName : Windows
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : True
Mode : d-----
BaseName : Windows
Target : {}
LinkType :
Name : Windows
Parent :
Exists : True
Root : C:\
FullName : C:\Windows
Extension :
CreationTime : 30/10/2015 06:28:30
CreationTimeUtc : 30/10/2015 06:28:30
LastAccessTime : 16/08/2016 17:32:04
LastAccessTimeUtc : 16/08/2016 16:32:04
LastWriteTime : 16/08/2016 17:32:04
LastWriteTimeUtc : 16/08/2016 16:32:04
Attributes : Directory
If you'd like to add properties to an existing object, you can use the Add-Member cmdlet. With PSObjects, values are
kept in a type of "Note Properties"
Add-Member -InputObject $object -Name "SomeNewProp" -Value "A value" -MemberType NoteProperty
# Returns
PS> $Object
Name ID Address SomeNewProp
---- -- ------- -----------
nem 12 A value
You can also add properties with Select-Object Cmdlet (so called calculated properties):
# Returns
PS> $newObject
Name ID Address SomeNewProp SomeOtherProp
---- -- ------- ----------- -------------
nem 12 A value Another value
Removing properties
You can use the Select-Object Cmdlet to remove properties from an object:
# Returns
PS> $object
Name SomeNewProp SomeOtherProp
---- ----------- -------------
nem A value Another value
Creating an object is simple. Most objects you create will be custom objects in PowerShell, and the type to use for
that is PSObject. PowerShell will also allow you to create any object you could create in .NET.
Option 1: New-Object
# Returns
PS> $newObject
Name ID Address
---- -- -------
nem 12
You can store the object in a variable by prefacing the command with $newObject =
You may also need to store collections of objects. This can be done by creating an empty collection variable, and
adding objects to the collection, like so:
$newCollection = @()
$newCollection += New-Object -TypeName PSObject -Property @{
Name = $env:username
ID = 12
Address = $null
}
You may then wish to iterate through this collection object by object. To do that, locate the Loop section in the
documentation.
Option 2: Select-Object
A less common way of creating objects that you'll still find on the internet is the following:
# Returns
PS> $newObject
Name ID Address
---- -- -------
nem 12
The ordered type accelerator forces PowerShell to keep our properties in the order that we defined them. You don't
need the ordered type accelerator to use [PSCustomObject]:
$newObject = [PSCustomObject][Ordered]@{
Name = $env:Username
ID = 12
Address = $null
}
# Returns
PS> $newObject
Name ID Address
---- -- -------
nem 12
#Nullable System.DateTime
[Nullable[datetime]]$nullableDate = Get-Date -Year 2012
$nullableDate
$nullableDate.GetType().FullName
$nullableDate = $null
$nullableDate
#Normal System.DateTime
[datetime]$aDate = Get-Date -Year 2013
$aDate
$aDate.GetType().FullName
$aDate = $null #Throws exception when PowerShell attempts to convert null to
[System.Collections.Generic.SortedDictionary[int, String]]$dict =
[System.Collections.Generic.SortedDictionary[int, String]]::new()
$dict.GetType().FullName
$dict.Add(1, 'a')
$dict.Add(2, 'b')
$dict.Add(3, 'c')
$dict
Key Value
--- -----
1 a
2 b
function Write-Greeting {
param(
[Parameter(Mandatory,Position=0)]
[String]$name,
[Parameter(Mandatory,Position=1)]
[Int]$age
)
"Hello $name, you are $age years old."
}
Simple function syntax (SFS) has very limited capabilities in comparison to the param block.
Though you can define parameters to be exposed within the function, you cannot specify Parameter Attributes,
utilize Parameter Validation, include [CmdletBinding()], with SFS (and this is a non-exhaustive list).
The order of the parameters on the invocation is matched to the order of the declaration in the function header (by
default), or can be specified using the Position Parameter Attribute (as shown in the advanced function example,
above).
get-help integration - the beginning of the function contains a comment block that is set up to be read by the
get-help cmdlet. The function block may be located at the end, if desired.
cmdletbinding - function will behave like a cmdlet
<#
.Synopsis
Short description
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Verb-Noun
{
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$true,
PositionalBinding=$false,
HelpUri = 'http://www.microsoft.com/',
ConfirmImpact='Medium')]
[Alias()]
[OutputType([String])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=0,
ParameterSetName='Parameter Set 1')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateCount(0,5)]
[ValidateSet("sun", "moon", "earth")]
[Alias("p1")]
$Param1,
Begin
{
}
Process
{
if ($pscmdlet.ShouldProcess("Target", "Operation"))
{
}
}
End
{
}
}
function Get-Greeting{
param
(
[Parameter(Mandatory=$true)]$name
)
"Hello World $name"
}
If the function is invoked without a value, the command line will prompt for the value:
$greeting = Get-Greeting
Instead of writing code within functions or scripts to validate parameter values, these ParameterAttributes will
throw if invalid values are passed.
ValidateSet
Sometimes we need to restrict the possible values that a parameter can accept. Say we want to allow only red,
green and blue for the $Color parameter in a script or function.
We can use the ValidateSet parameter attribute to restrict this. It has the additional benefit of allowing tab
completion when setting this argument (in some environments).
param(
[ValidateSet('red','green','blue',IgnoreCase)]
[string]$Color
ValidateRange
This method of parameter validation takes a min and max Int32 value, and requires the parameter to be within that
range.
param(
[ValidateRange(0,120)]
[Int]$Age
)
ValidatePattern
This method of parameter validation accepts parameters that match the regex pattern specified.
param(
[ValidatePattern("\w{4-6}\d{2}")]
[string]$UserName
)
ValidateLength
This method of parameter validation tests the length of the passed string.
param(
[ValidateLength(0,15)]
[String]$PhoneNumber
)
ValidateCount
This method of parameter validation tests the amount of arguments passed in, for example, an array of strings.
param(
[ValidateCount(1,5)]
[String[]]$ComputerName
)
ValidateScript
Finally, the ValidateScript method is extraordinarily flexible, taking a scriptblock and evaluating it using $_ to
represent the passed argument. It then passes the argument if the result is $true (including any output as valid).
param(
[ValidateScript({Test-Path $_})]
[IO.FileInfo]$Path
)
param(
[ValidateScript({Get-ADUser $_})]
[String]$UserName
)
param(
[ValidateScript({
$AnHourAgo = (Get-Date).AddHours(-1)
if ($_ -lt $AnHourAgo.AddMinutes(5) -and $_ -gt $AnHourAgo.AddMinutes(-5)) {
$true
} else {
throw "That's not within five minutes. Try again."
}
})]
[String]$TimeAboutAnHourAgo
)
function Get-Greeting{
"Hello World"
}
# demonstrate output
$greeting
Get-Greeting
Get-Greeting is the name of the function. Any time that function needs to be used in the script, the function can be
called by means of invoking it by name.
If the above code is executed in the ISE, the results would be something like:
Hello World
Hello World
In PowerShell 5.0+ you can list available constructors by calling the static new-method without parentheses.
PS> [DateTime]::new
OverloadDefinitions
-------------------
datetime new(long ticks)
datetime new(long ticks, System.DateTimeKind kind)
datetime new(int year, int month, int day)
datetime new(int year, int month, int day, System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second)
datetime new(int year, int month, int day, int hour, int minute, int second, System.DateTimeKind
kind)
datetime new(int year, int month, int day, int hour, int minute, int second,
System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.DateTimeKind kind)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.Globalization.Calendar calendar, System.DateTimeKind kind)
This is the same technique that you can use to list overload definitions for any method
> 'abc'.CompareTo
OverloadDefinitions
-------------------
int CompareTo(System.Object value)
int CompareTo(string strB)
int IComparable.CompareTo(System.Object obj)
int IComparable[string].CompareTo(string other)
For earlier versions you can create your own function to list available constructors:
function Get-Constructor {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[type]$type
)
Process {
$type.GetConstructors() |
Format-Table -Wrap @{
n="$($type.Name) Constructors"
Usage:
Get-Constructor System.DateTime
#Or [datetime] | Get-Constructor
DateTime Constructors
---------------------
Int64 ticks
Int64 ticks, System.DateTimeKind kind
Int32 year, Int32 month, Int32 day
Int32 year, Int32 month, Int32 day, System.Globalization.Calendar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, System.DateTimeKind
kind
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second,
System.Globalization.Calendar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond,
System.DateTimeKind kind
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond,
System.Globalization.Cal
endar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond,
System.Globalization.Cal
endar calendar, System.DateTimeKind kind
$x = [Person]::new()
$x.FirstName = "Jane"
$x.LastName = "Doe"
$greeting = $x.Greeting() # "Greetings, Jane Doe!"
Person([string] $Name) {
$this.Name = $Name
}
This will return all members of the type instance. Here is a part of a sample output for String instance
TypeName: System.String
# Static property
static [hashtable] $P2
# Constructor
TypeName ([string] $s)
{
$this.P1 = $s
}
# Static method
static [void] MemberMethod1([hashtable] $h)
{
[TypeName]::P2 = $h
}
# Instance method
[int] MemberMethod2([int] $i)
{
[string] GetMessage()
{
return ("Message: {0}" -f $this.Message)
}
}
}
$Inherit = [ChildClass]::new()
Modules can expose command aliases, functions, variables, classes, and more.
Every good PowerShell module has a module manifest. The module manifest simply contains metadata about a
PowerShell module, and doesn't define the actual contents of the module.
The manifest file is a PowerShell script file, with a .psd1 file extension, that contains a HashTable. The HashTable in
the manifest must contain specific keys, in order for PowerShell to correctly interpret it as a PowerShell module file.
The example above provides a list of the core HashTable keys that make up a module manifest, but there are many
others. The New-ModuleManifest command helps you create a new module manifest skeleton.
return $x + $y
}
This is a simple example of what a PowerShell script module file might look like. This file would be called
MyCoolModule.psm1, and is referenced from the module manifest (.psd1) file. You'll notice that the Export-
ModuleMember command enables us to specify which functions in the module we want to "export," or expose, to the
user of the module. Some functions will be internal-only, and shouldn't be exposed, so those would be omitted
from the call to Export-ModuleMember.
To export a variable from a module, you use the Export-ModuleMember command, with the -Variable parameter.
Remember, however, that if the variable is also not explicitly exported in the module manifest (.psd1) file, then the
variable will not be visible to the module consumer. Think of the module manifest like a "gatekeeper." If a function
or variable isn't allowed in the module manifest, it won't be visible to the module consumer.
Note: Exporting a variable is similar to making a field in a class public. It is not advisable. It would be better to
expose a function to get the field and a function to set the field.
\MyCoolModule
\Functions
Function1.ps1
Function2.ps1
Function3.ps1
MyCoolModule.psd1
MyCoolModule.psm1
Inside your MyCoolModule.psm1 file, you could insert the following code:
This would dot-source the individual function files into the .psm1 module file.
A module called foo, in a folder called foo will be found with Import-Module foo
In that folder, PowerShell will look for a module manifest (foo.psd1), a module file (foo.psm1), a DLL (foo.dll).
To override this behavior, you can use the Export-ModuleMember cmdlet. It has parameters called -Function, -
Variable, and -Alias which allow you to specify exactly which members are exported.
It is important to note that if you use Export-ModuleMember, only the items you specify will be visible.
If you are in ISE you can use the built in editor C:>psEdit $profile
An easy way to get started with your personal profile for the current host is to save some text to path stored in the
$profile-variable
Further modification to the profile can be done using PowerShell ISE, notepad, Visual Studio Code or any other
editor.
The $profile-variable returns the current user profile for the current host by default, but you can access the path
to the machine-policy (all users) and/or the profile for all hosts (console, ISE, 3rd party) by using its properties.
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost :
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\user\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length : 75
PS> $PROFILE.AllUsersAllHosts
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
It simply output the folder content with the selected properties. Something like,
What if I want to display the file size in KB ? This is where calcualted properties comes handy.
Which produces,
The Expression is what holds the calculation for calculated property. And yes, it can be anything!
or by file path:
If you want to know which methods are available you can use:
PS C:\> [System.Math]::Floor(9.42)
9
PS C:\> [System.Math]::Ceiling(9.42)
10
PS C:\> [System.Math]::Pow(4,3)
64
PS C:\> [System.Math]::Sqrt(49)
7
PS C:\> [guid]::NewGuid()
Guid
----
8874a185-64be-43ed-a64c-d2fe4b6e31bc
PS C:\> New-Guid
[guid]::NewGuid().Guid
This example retrieves the list of child items (directories and files) from the folder where the script file resides.
The $PSScriptRoot automatic variable is $null if used from outside a PowerShell code file. If used inside a
PowerShell script, it automatically defined the fully-qualified filesystem path to the directory that contains the script
file.
In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell
3.0, it is valid in all scripts.
Contains an array of the undeclared parameters and/or parameter values that are passed to a function, script, or
script block. When you create a function, you can declare the parameters by using the param keyword or by adding
a comma-separated list of parameters in parentheses after the function name.
In an event action, the $Args variable contains objects that represent the event arguments of the event that is being
processed. This variable is populated only within the Action block of an event registration command. The value of
this variable can also be found in the SourceArgs property of the PSEventArgs object
(System.Management.Automation.PSEventArgs) that Get-Event returns.
Same as $_. Contains the current object in the pipeline object. You can use this variable in commands that perform
an action on every object or on selected objects in a pipeline.
Section 18.4: $?
Get-Process -Name doesnotexist
Write-Host -Object "Was the last operation successful? $?"
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it
failed.
Contains an array of error objects that represent the most recent errors. The most recent error is the first error
object in the array ($Error[0]).
To prevent an error from being added to the $Error array, use the ErrorAction common parameter with a value of
Ignore. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
Section 19.2: $?
Contains status of the last operation. When there is no error, it is set to True:
PS C:\> wrt-host
wrt-host : The term 'wrt-host' is not recognized as the name of a cmdlet, function, script file, or
operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ wrt-host
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (wrt-host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\> $?
False
When we use the same array as the source for ForEach-Object, it will process all three items (including $null):
Be careful! This means that ForEach-Object WILL process even $null all by itself:
Usage hints: When using the $error variable in a format cmdlet (e.g. format-list), be aware to use the -Force switch.
Otherwise the format cmdlet is going to output the $errorcontents in above shown manner.
PS C:\> $pid
26080
Note that you have to specify the dollar sign as the first character (which is different from C#).
Notice that when you use boolean true/false in your code you write $true or $false, but when Powershell returns a
boolean, it looks like True or False
$PSItem and $_ are identical and can be used interchangeably, but $_ is by far the most commonly used.
$PSVersionTable.PSVersion
# result :
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
Splatting can make a command more readable and allows you to reuse parameters in multiple command calls.
$splat = @{
Class = "Win32_SystemEnclosure"
Property = "Manufacturer"
ErrorAction = "Stop"
}
However, if the splat is not indented for reuse, you may not wish to declare it. It can be piped instead:
@{
ComputerName = $env:COMPUTERNAME
Class = "Win32_SystemEnclosure"
Property = "Manufacturer"
ErrorAction = "Stop"
} | % { Get-WmiObject @_ }
Get-Process -FileVersionInfo
$MyParameters = @{
FileVersionInfo = $true
}
Get-Process @MyParameters
Note: This is useful because you can create a default set of parameters and make the call many times like this
$MyParameters = @{
FileVersionInfo = $true
}
Function Outer-Method
{
Param
(
[string]
$First,
[string]
$Second
)
Inner-Method @PSBoundParameters
}
Function Inner-Method
{
Param
(
[string]
$Second
)
$parameters = @{
First = "Hello"
Second = "World"
}
Outer-Method @parameters
$MyParameters = @{
Name = "iexplore"
FileVersionInfo = $true
}
Get-Process @MyParameters
Without splatting:
$MyParameters = @{
ComputerName = "StackOverflow-PC"
}
The Cmdlet sends objects down the primary pipeline, also known as the "output stream" or the "success pipeline."
To send error objects down the error pipeline, use Write-Error.
# 2.) Output to the console since Write-Output is the last command in the pipeline
Write-Output 'Hello world'
# 3.) 'Write-Output' CmdLet missing, but the output is still considered to be 'Write-Output'
'Hello world'
1. The Write-Output cmdlet sends the specified object down the pipeline to the next command.
2. If the command is the last command in the pipeline, the object is displayed in the console.
3. The PowerShell interpreter treats this as an implicit Write-Output.
Because Write-Output's default behavior is to display the objects at the end of a pipeline, it is generally not
necessary to use the Cmdlet. For example, Get-Process | Write-Output is equivalent to Get-Process.
$VerbosePreference = "SilentlyContinue"
$InformationPreference = "SilentlyContinue"
$DebugPreference = "SilentlyContinue"
$ProgressPreference = "Continue"
$WarningPreference = "Continue"
The preference variable controls how the message and subsequent execution of the script are handled;
$InformationPreference = "SilentlyContinue"
Write-Information "This message will not be shown and execution continues"
$InformationPreference = "Inquire"
Write-Information "This message is shown and execution will optionally continue"
$InformationPreference = "Stop"
Write-Information "This message is shown and execution terminates"
$host.PrivateData.ErrorBackgroundColor = "Black"
$host.PrivateData.ErrorForegroundColor = "Red"
A useful technique for Exchange Server administrators is to be able to send email messages via SMTP from
PowerShell. Depending on the version of PowerShell installed on your computer or server, there are multiple ways
to send emails via PowerShell. There is a native cmdlet option that is simple and easy to use. It uses the cmdlet
Send-MailMessage.
function Send_mail {
#Define Email settings
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Txt_Body = Get-Content $Txt_File -RAW
$Body = $Body_Custom + $Txt_Body
$Subject = "Email Subject"
$SMTPServer = "smtpserver.domain.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Send_mail
Enter-PSSession 192.168.1.1
The above method creates a temporary session and closes it right after the command or scriptblock ends.
To leave the session open and run other command in it later, you need to create a remote session first:
Then you can use this session each time you invoke commands on the remote computer:
If you need to use different Credentials, you can add them with the -Credential Parameter:
$Cred = Get-Credential
Invoke-Command -Session $Session -Credential $Cred -ScriptBlock {...}
Note:
It is important to know that remoting serializes PowerShell objects on the remote system and deserializes
them on your end of the remoting session, i.e. they are converted to XML during transport and lose all of
their methods.
TypeName: Deserialized.System.Management.ManagementObject#root\cimv2\Win32_Printer
TypeName: System.Management.ManagementObject#root\cimv2\Win32_Printer
Argument Usage
To use arguments as parameters for the remote scripting block, one might either use the ArgumentList parameter
of Invoke-Command, or use the $Using: syntax.
Using ArgumentList with unnamed parameters (i.e. in the order they are passed to the scriptblock):
$servicesToShow = "service1"
$fileName = "C:\temp\servicestatus.csv"
Invoke-Command -Session $session -ArgumentList $servicesToShow,$fileName -ScriptBlock {
Write-Host "Calling script block remotely with $($Args.Count)"
Get-Service -Name $args[0]
Remove-Item -Path $args[1] -ErrorAction SilentlyContinue -Force
}
$servicesToShow = "service1"
$fileName = "C:\temp\servicestatus.csv"
Invoke-Command -Session $session -ArgumentList $servicesToShow,$fileName -ScriptBlock {
Param($serviceToShowInRemoteSession,$fileToDelete)
$servicesToShow = "service1"
$fileName = "C:\temp\servicestatus.csv"
Invoke-Command -Session $session -ScriptBlock {
Get-Service $Using:servicesToShow
Remove-Item -Path $fileName -ErrorAction SilentlyContinue -Force
}
Enable-PSRemoting -Force
For servers in an AD Domain the PS remoting authentication is done through Kerberos ('Default'), or NTLM
('Negotiate'). If you want to allow remoting to a non-domain server you have two options.
Either set up WSMan communication over HTTPS (which requires certificate generation) or enable basic
authentication which sends your credentials across the wire base64-encoded (that's basically the same as plain-text
so be careful with this).
In either case you'll have to add the remote systems to your WSMan trusted hosts list.
Then on the computer you wish to connect from, you must tell it to trust the computer you're connecting to.
Set-Item WSMan:\localhost\Client\TrustedHosts *
Multiple active PSSessions can become a strain on resources, particularly for long running or interlinked scripts
that create hundreds of PSSessions in a single PowerShell session.
It is best practise to explicitly remove each PSSession after it is finished being used. [1]
The following code template utilises try-catch-finally in order to achieve the above, combining error handling
with a secure way to ensure all created PSSessions are removed when they are finished being used:
try
{
$session = New-PSsession -Computername "RemoteMachineName"
Invoke-Command -Session $session -ScriptBlock {write-host "This is running on
$ENV:ComputerName"}
}
catch
{
Write-Output "ERROR: $_"
}
finally
{
if ($session)
{
Remove-PSSession $session
}
}
References: [1]
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/new-pssession
Note, that the begin and end structures of the function are optional when pipelining, but that process is required
when using ValueFromPipeline or ValueFromPipelineByPropertyName.
function Write-FromPipeline{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
$myInput
)
begin {
Write-Verbose -Message "Beginning Write-FromPipeline"
}
process {
Write-Output -InputObject $myInput
}
end {
Write-Verbose -Message "Ending Write-FromPipeline"
}
}
$foo = 'hello','world',1,2,3
Output:
function Write-FromPipeline {
param(
$foo | Write-FromPipeline
Output:
Hello World!
Note: In PowerShell 3.0 and above, Default Values for ParameterAttributes is supported. In earlier versions, you
must specify ValueFromPipeline=$true.
It will take a while to fetch full directory list of your C: drive. If you run it as Job then the console will get the control
back and you can capture the result later on.
Or
$username = "Domain\Username"
$password = "password"
$secPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,
$secPassword)
Start-Job -ScriptBlock {Get-Process} -Credential $credentials
Get-Job
Stopping a job (completes all tasks that are pending in that job queue before ending):
$job | Stop-Job
$job | Remove-Job
$job | Suspend-Job
$job | Resume-Job
Pipeline cmdlets (ex: ForEach-Object, Where-Object, etc) operate on closures. The return here will only move to the
next item on the pipeline, not exit processing. You can use break instead of return if you want to exit processing.
The following methods will have the same values on the pipeline
function foo {
$a = "Hello"
return $a
}
function bar {
$a = "Hello"
$a
return
}
function quux {
$a = "Hello"
$a
}
Like this:
Test-Function
Will return:
Start
The newly created registry key (this is because there are some statements that create output that you may
not be expecting)
Yes, it worked!
Start
Damn, it didn't work!
Function Test-Function
{
Param
(
[switch]$ExceptionalReturn
)
. {
"Start"
if($ExceptionalReturn)
{
$Return = "Damn, it didn't work!"
Return
}
New-ItemProperty -Path "HKCU:\" -Name "test" -Value "TestValue" -Type "String"
$Return = "Yes, it worked!"
Return
} | Out-Null
Return $Return
}
Test-Function
Will return:
Yes, it worked!
With this trick you can control the returned output even if you are not sure what will each statement will spit out.
.{<Statements>} | Out-Null
function bar {
[System.Collections.ArrayList]$MyVariable = @()
$MyVariable.Add("a") | Out-Null
$MyVariable.Add("b") | Out-Null
$MyVariable
}
The Out-Null is necessary because the .NET ArrayList.Add method returns the number of items in the collection
after adding. If omitted, the pipeline would have contained 1, 2, "a", "b"
function bar
{
# New-Item cmdlet returns information about newly created file/folder
New-Item "test1.txt" | out-null
New-Item "test2.txt" > $null
[void](New-Item "test3.txt")
$tmp = New-Item "test4.txt"
}
Note: to learn more about why to prefer > $null, see [topic not yet created].
String,DateTime,Integer
First,2016-12-01T12:00:00,30
Second,2015-12-01T12:00:00,20
Third,2015-12-01T12:00:00,20
One can import the CSV rows in PowerShell objects using the Import-Csv command
Using Foreach-Object:
#Output object
$_
}
Output:
# First Method
$xdoc = New-Object System.Xml.XmlDocument
$file = Resolve-Path(".\file.xml")
$xdoc.load($file)
# Second Method
[xml] $xdoc = Get-Content ".\file.xml"
# Third Method
$xdoc = [xml] (Get-Content ".\file.xml")
PS C:\> $xml.people
person
--------
{Jon Lajoie, Lord Gaben, Gordon Freeman}
PS C:\> $xml.people.person
id name age
-- ---- ---
101 Jon Lajoie 22
102 Lord Gaben 65
103 Gordon Freeman 29
PS C:\> $xml.people.person[0].name
Jon Lajoie
PS C:\> $xml.people.person[1].age
65
PS C:\> $xml.SelectNodes("//people")
person
--------
{Jon Lajoie, Lord Gaben, Gordon Freeman}
PS C:\> $xml.SelectNodes("//people//person")
id name age
-- ---- ---
101 Jon Lajoie 22
102 Lord Gaben 65
103 Gordon Freeman 29
PS C:\> $xml.SelectSingleNode("people//person[1]//name")
Jon Lajoie
PS C:\> $xml.SelectSingleNode("people//person[2]//age")
65
PS C:\> $xml.SelectSingleNode("people//person[3]//@id")
103
id name
-- ----
101 Jon Lajoie
102 Lord Gaben
103 Gordon Freeman
$xmlWriter.WriteElementString("Property1","Value 1")
$xmlWriter.WriteElementString("Property2","Value 2")
First, let's define a sample XML document named "books.xml" in our current directory:
New Data
What we want to do is add a few new books to this document, let's say Patriot Games by Tom Clancy (yes, I'm a fan
of Clancy's works ^__^) and a Sci-Fi favourite: The Hitchhiker's Guide to the Galaxy by Douglas Adams mainly because
Somehow we've acquired the data for the new books and saved them as a list of PSCustomObjects:
$newBooks = @(
[PSCustomObject] @{
"Title" = "Patriot Games";
"Author" = "Tom Clancy";
"PageCount" = 540;
"Publishers" = @(
[PSCustomObject] @{
"ISBN" = "978-0-39-913241-4";
"Year" = "1987";
"First" = $True;
"Name" = "Putnam";
"Binding" = "Hardcover";
}
);
"Characters" = @(
"Jack Ryan", "Prince of Wales", "Princess of Wales",
"Robby Jackson", "Cathy Ryan", "Sean Patrick Miller"
);
"film" = $True;
},
[PSCustomObject] @{
"Title" = "The Hitchhiker's Guide to the Galaxy";
"Author" = "Douglas Adams";
"PageCount" = 216;
"Publishers" = @(
[PSCustomObject] @{
"ISBN" = "978-0-33-025864-7";
"Year" = "1979";
"First" = $True;
"Name" = "Pan Books";
"Binding" = "Hardcover";
}
);
"Characters" = @(
"Arthur Dent", "Marvin", "Zaphod Beeblebrox", "Ford Prefect",
"Trillian", "Slartibartfast", "Dirk Gently"
);
"film" = $True;
}
);
Templates
Now we need to define a few skeleton XML structures for our new data to go into. Basically, you want to create a
skeleton/template for each list of data. In our example, that means we need a template for the book, characters,
and publishers. We can also use this to define a few default values, such as the value for the film tag.
Now that we're all set-up with our sample data, let's add the custom objects to the XML Document Object.
# Outputs:
# title author ISBN
# ----- ------ ----
# Of Mice And Men John Steinbeck 978-88-58702-15-4
# The Hunt for Red October Tom Clancy 978-08-70212-85-7
# Outputs:
# Title Author ISBN
# ----- ------ ----
# Patriot Games Tom Clancy 978-0-39-913241-4
# The Hitchhiker's Guide to the Galaxy Douglas Adams 978-0-33-025864-7
# Add the template for a book as a new node to the root element
[void]$root.AppendChild($xml.ImportNode($t_book.book, $true));
# Update the parameters of that new element to match our current new book data
$newElement.title = [String]$book.Title;
$newElement.author = [String]$book.Author;
$newElement.pageCount = [String]$book.PageCount;
$newElement.film = [String]$book.Film;
# Iterate through the properties that are Children of our new Element:
ForEach ( $publisher in $book.Publishers ) {
[void]$newElement.SelectSingleNode("publishers").AppendChild($xml.ImportNode($t_publisher.publisher
, $true));
# Update the attribute and text values of our new XML Element to match our new data
$newPublisherElement = $newElement.SelectSingleNode("publishers/publisher[last()]");
$newPublisherElement.year = [String]$publisher.Year;
$newPublisherElement.name = [String]$publisher.Name;
$newPublisherElement.binding = [String]$publisher.Binding;
$newPublisherElement.isbn = [String]$publisher.ISBN;
If ( $publisher.first ) {
$newPublisherElement.first = "True";
}
}
# Update the attribute and text values to match our new data
$characterElement.name = [String]$character;
}
}
# Outputs:
# title author ISBN
# ----- ------ ----
# Of Mice And Men John Steinbeck 978-88-58702-15-4
# The Hunt for Red October Tom Clancy 978-08-70212-85-7
# Patriot Games Tom Clancy 978-0-39-913241-4
# The Hitchhiker's Guide to the Galaxy Douglas Adams 978-0-33-025864-7
Profit
While this may not be the procedure for everyone I found it to help avoid a whole bunch of
[void]$xml.SelectSingleNode("/complicated/xpath/goes[here]").AppendChild($xml.CreateElement("newEle
mentName") followed by $xml.SelectSingleNode("/complicated/xpath/goes/here/newElementName") =
$textValue
I think the method detailed in the example is cleaner and easier to parse for normal humans.
Improvements
Invoke-RestMethod @params
$Inst="ServerInstance"
$DbName="DatabaseName
$UID="User ID"
$Password="Password"
Invoke-Sqlcmd2 -Serverinstance $Inst -Database $DBName -query $Query -Username $UID -Password
$Password
$Inst="ServerInstance"
$DbName="DatabaseName
$UID="User ID"
$Password="Password"
Invoke-Sqlcmd2 -Serverinstance $Inst -Database $DBName -query $Query -Username $UID -Password
$Password
#Sample text
$text = @"
This is (a) sample
text, this is
a (sample text)
"@
To determine if a string matches a pattern using the built-in -matches operator, use the syntax 'input' -match
'pattern'. This will return true or false depending on the result of the search. If there was match you can view
the match and groups (if defined in pattern) by accessing the $Matches-variable.
> $Matches
Name Value
---- -----
0 (a)
You can also use -match to filter through an array of strings and only return the strings containing a match.
PowerShell 2.0 introduced a new cmdlet for searching through text using regex. It returns a MatchInfo object per
textinput that contains a match. You can access it's properties to find matching groups etc.
> $m
> $m | Format-List *
Like -match, Select-String can also be used to filter through an array of strings by piping an array to it. It creates a
MatchInfo-object per string that includes a match.
IgnoreCase : True
LineNumber : 1
Line : This is (a) sample
Filename : InputStream
Path : InputStream
Pattern : \(.*?\)
Context :
Matches : {(a)}
IgnoreCase : True
LineNumber : 3
Line : a (sample text)
Filename : InputStream
Path : InputStream
Pattern : \(.*?\)
Context :
Matches : {(sample text)}
Select-String can also search using a normal text-pattern (no regex) by adding the -SimpleMatch switch.
Using [RegEx]::Match()
You can also use the static Match() method available in the .NET [RegEx]-class.
> [regex]::Match($text,$pattern)
Groups : {(a)}
Success : True
Captures : {(a)}
Index : 8
Length : 3
Value : (a)
#Sample text
$text = @"
This is (a) sample
text, this is
a (sample text)
"@
The -replace operator in PowerShell can be used to replace text matching a pattern with a new value using the
syntax 'input' -replace 'pattern', 'newvalue'.
Replacing matches can also be done using the Replace() method in the [RegEx] .NET class.
In PowerShell, a MatchEvaluator is as simple as a scriptblock with a single parameter that contains a Match-object
for the current match. The output of the action will be the new value for that specific match. MatchEvalutor can be
used with the [Regex]::Replace() static method.
#Sample text
$text = @"
This is (a) sample
text, this is
a (sample text)
"@
$MatchEvalutor = {
Output:
This is 1 sample
text, this is
a 11
$MatchEvalutor = {
param($match)
Output:
To use these characters, as a .,+ etc., in a pattern, you need to escape them to remove their special meaning. This is
done by using the escape character which is a backslash \ in regex. Example: To search for +, you would use the
pattern \+.
It can be hard to remember all special characters in regex, so to escape every special character in a string you want
to search for, you could use the [RegEx]::Escape("input") method.
> [regex]::Escape("(foo)")
\(foo\)
> [regex]::Escape("1+1.2=2.2")
1\+1\.2=2\.2
Using Select-String
You can find all matches (global match) by adding the -AllMatches switch to Select-String.
> $m | Format-List *
IgnoreCase : True
LineNumber : 1
Line : This is (a) sample
text, this is
a (sample text)
Filename : InputStream
Path : InputStream
Pattern : \(.*?\)
Context :
Matches : {(a), (sample text)}
Groups : {(a)}
Success : True
Captures : {(a)}
Index : 8
Length : 3
Value : (a)
Using [RegEx]::Matches()
The Matches() method in the .NET `[regex]-class can also be used to do a global search for multiple matches.
> [regex]::Matches($text,$pattern)
Groups : {(a)}
Success : True
Captures : {(a)}
Index : 8
Length : 3
(a)
(sample text)
Get-Alias
Keep in mind that any alias you create will be persisted only in current session. When you start new session you
need to create your aliases again. Powershell Profiles (see [topic not yet created]) are great for these purposes.
Please note that for brevity this example does not contain any executive code (simulated with Start-Sleep). However it is
possible to run it directly as is and then modify and play with it.
1..100 | foreach-object {
Write-Progress -Activity "Copying contents of the file $fileName" -Status "$_ %" -Id 2
-ParentId 1 -PercentComplete $_ -CurrentOperation "Copying $_. line"
Start-Sleep -Milliseconds 20 # sleep simulates working code, replace this line with
your executive code (i.e. file copying)
}
Start-Sleep -Milliseconds 500 # sleep simulates working code, replace this line with your
Please note that for brevity this example does not contain any executive code (simulated with Start-Sleep). However it is
possible to run it directly as is and then modify and play with it.
-Command <string>
You can specify commands to executed on launch as a string. Multiple semicolon ;-separated statements may be
executed.
-Command { scriptblock }
The -Command parameter also supports a scriptblock input (one or multiple statements wrapped in braces { #code
}. This only works when calling PowerShell.exe from another Windows PowerShell-session.
You can pass in commands from the standard input by using -Command -. The standard input can come from echo,
reading a file, a legacy console application etc.
Basic script
MyScript.ps1
(Get-Date).ToShortDateString()
"Hello World"
Output:
You can add parameters and/or arguments after filepath to use them in the script. Arguments will be used as
values for undefined/available script-parameters, the rest will be available in the $args-array
MyScript.ps1
param($Name)
Output:
Further details on how to use verbs can be found at Approved Verbs for Windows PowerShell
Be consistent with the nouns. For instance Find-Package needs a provider the noun is PackageProvider not
ProviderPackage.
GUI applications launch in a different process, and will immediately return control to the PowerShell host.
Sometimes you need the application to finish processing before the next PowerShell statement must be executed.
This can be achieved by piping the application output to $null (3) or by using Start-Process with the -Wait switch (5).
Stream 2 contains System.Management.Automation.ErrorRecord objects. Note that some applications like git.exe
use the "error stream" for informational purposes, that are not necessarily errors at all. In this case it is best to look
at the exit code to determine whether the error stream should be interpreted as errors.
PowerShell understands these streams: Output, Error, Warning, Verbose, Debug, Progress. Native applications
commonly use only these streams: Output, Error, Warning.
In PowerShell 5, all streams can be redirected to the standard output/success stream (6).
In earlier PowerShell versions, only specific streams can be redirected to the standard output/success stream (7). In
this example, the "error stream" will be redirected to the output stream.
These are built-in PowerShell variables that provide additional information about the most recent error.
$LastExitCode is the final exit code of the last native application that was executed. $? and $Error[0] is the last
error record that was generated by PowerShell.
After trying to run this script in lower version, you will see this error message
.\script.ps1 : The script 'script.ps1' cannot be run because it contained a "#requires" statement at line 1
for Windows PowerShell version 5.0. The version required by the script does not match the currently
running version of Windows PowerShell version 2.0.
After trying to run this script without admin privileges, you will see this error message
.\script.ps1 : The script 'script.ps1' cannot be run because it contains a "#requires" statement for running
as Administrator. The current Windows PowerShell session is not running as Administrator. Start
Windows PowerShell by using the Run as Administrator option, and then try running the script again.
Beginning with PowerShell 3.0, you can download and update the offline help documentation using a single cmdlet.
Update-Help
To update help on multiple computers (or computers not connected to the internet).
Get-Help about_Jobs
You can search for topics using wildcards. If you want to list available help topics with a title starting with about_,
try:
Get-Help about_*
Get-Help Select-Object
PowerShell Functions are to avoid repetitive codes. Refer [PS Functions][1] [1]: PowerShell Functions
PowerShell Scripts are used for automating administrative tasks which consists of command-line shell and
associated cmdlets built on top of .NET Framework.
Functions are created using the function keyword, followed by a single-word name and a script block containing
the code to executed when the function name is called.
function NameOfFunction {
Your code
}
Demo
function HelloWorld {
Write-Host "Greetings from PowerShell!"
}
Usage:
> HelloWorld
Greetings from PowerShell!
Demo
MyFirstScript.ps1:
You can run a script by entering the path to the file using an:
Usage:
> .\MyFirstScript.ps1
Hello World!
4
A script can also import modules, define its own functions etc.
MySecondScript.ps1:
function HelloWorld {
Write-Host "Greetings from PowerShell!"
}
HelloWorld
Write-Host "Let's get started!"
2+2
HelloWorld
Usage:
> .\MySecondScript.ps1
Greetings from PowerShell!
Let's get started!
4
Greetings from PowerShell!
One or more code files with the .psm1 file extension containing functions or binary assemblies (.dll)
containing cmdlets
A module manifest .psd1 describing the modules name, version, author, description, which
functions/cmdlets it provides etc.
Other requirements for it to work incl. dependencies, scripts etc.
Examples of modules:
To make it easy for PowerShell to find and import a module, it is often placed in one of the known PowerShell
module-locations defined in $env:PSModulePath.
Demo
Get-Module -ListAvailable
Import-Module Hyper-V
Simple version:
<#
.Synopsis
Short description
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
#>
function Verb-Noun
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$Param1,
Complete version:
<#
.Synopsis
Short description
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Verb-Noun
{
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$true,
PositionalBinding=$false,
HelpUri = 'http://www.microsoft.com/',
ConfirmImpact='Medium')]
[OutputType([String])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=0,
ParameterSetName='Parameter Set 1')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateCount(0,5)]
[ValidateSet("sun", "moon", "earth")]
[Alias("p1")]
$Param1,
Begin
{
}
Process
{
if ($pscmdlet.ShouldProcess("Target", "Operation"))
{
}
}
End
{
}
}
Value of this parameter will determine how the cmdlet will handle non-terminating errors (those generated from
Write-Error for example; to learn more about error handling see [topic not yet created]).
-ErrorAction Continue
This option will produce an error message and will continue with execution.
-ErrorAction Ignore
This option will not produce any error message and will continue with execution. Also no errors will be added to
$Error automatic variable.
This option was introduced in v3.
-ErrorAction Inquire
This option will produce an error message and will prompt user to choose an action to take.
-ErrorAction SilentlyContinue
This option will not produce an error message and will continue with execution. All errors will be added to $Error
automatic variable.
-ErrorAction Stop
-ErrorAction Suspend
Only available in Powershell Workflows. When used, if the command runs into an error, the workflow is suspended.
This allows investigation of such error and gives a possibility to resume the workflow. To learn more about
Workflow system, see [topic not yet created].
The examples will explain the use and reason of a parameter set.
Function Do-Something
{
Param
(
[Parameter(Mandatory=$true)]
[String]$SomeThingToDo,
[Parameter(ParameterSetName="Credentials", mandatory=$false)]
[String]$Computername = "LocalHost",
[Parameter(ParameterSetName="Credentials", mandatory=$true)]
[String]$User,
[Parameter(ParameterSetName="Credentials", mandatory=$true)]
[SecureString]$Password
)
#Do something
}
# This will not work he will ask for user and password
Do-Something -SomeThingToDo 'get-help about_Functions_Advanced' -ComputerName
#Do something
}
# This will not work because you can not use the combination Silently and Loudly
Do-Something -SomeThingToDo 'get-help about_Functions_Advanced' -Silently -Loudly
function MyTestFunction
{
[CmdletBinding(DefaultParameterSetName='DefaultConfiguration')]
Param
(
[Parameter(Mandatory=$true)][int]$SomeUsefulNumber
)
DynamicParam
{
$paramDictionary = New-Object -Type
System.Management.Automation.RuntimeDefinedParameterDictionary
$attributes = New-Object System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "__AllParameterSets"
$attributes.Mandatory = $true
$attributeCollection = New-Object -Type
System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
# If "SomeUsefulNumber" is greater than 5, then add the "MandatoryParam1" parameter
if($SomeUsefulNumber -gt 5)
{
# Create a mandatory string parameter called "MandatoryParam1"
$dynParam1 = New-Object -Type
System.Management.Automation.RuntimeDefinedParameter("MandatoryParam1", [String],
$attributeCollection)
# Add the new parameter to the dictionary
$paramDictionary.Add("MandatoryParam1", $dynParam1)
}
return $paramDictionary
}
process
{
Write-Host "SomeUsefulNumber = $SomeUsefulNumber"
# Notice that dynamic parameters need a specific syntax
Write-Host ("MandatoryParam1 = {0}" -f $PSBoundParameters.MandatoryParam1)
}
Usage:
In the second usage example, you can clearly see that a parameter is missing.
Dynamic parameters are also taken into account with auto completion.
Here's what happens if you hit ctrl + space at the end of the line:
[xml]$XAMLWindow = '
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto"
SizeToContent="WidthAndHeight"
Title="Get-Service">
<ScrollViewer Padding="10,10,10,0" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="10,10,0,10">ComputerName:</Label>
<TextBox Name="Input" Margin="10" Width="250px"></TextBox>
</StackPanel>
<DockPanel>
<Button Name="ButtonGetService" Content="Get-Service" Margin="10" Width="150px"
IsEnabled="false"/>
<Button Name="ButtonClose" Content="Close" HorizontalAlignment="Right" Margin="10"
Width="50px"/>
</DockPanel>
</StackPanel>
</ScrollViewer >
</Window>
'
[System.Windows.MessageBox]::Show($_.exception.message,"Error",[System.Windows.MessageBoxButton]::O
K,[System.Windows.MessageBoxImage]::Error)
}
})
This creates a dialog window which allows the user to select a computer name, then will display a table of services
and their statuses on that computer.
This example uses WPF rather than Windows Forms.
With [System.Web.HttpUtility]::UrlEncode(), you will notice that spaces are turned into plus signs (+) instead of
%20:
https://example.vertigion.com/foos?
foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&
complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1
# url1: https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Ftest.com%3Ftest%3Dmy%20value
[uri]::UnescapeDataString($url1)
# Returns: http://test.com?test=my value
# url2: http://test.com?test=my%20value
[uri]::UnescapeDataString($url2)
# Returns: http://test.com?test=my value
With [uri]::EscapeDataString(), you will notice that the apostrophe (') was not encoded:
https://example.vertigion.com/foos?
foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar'%22&
complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo'%22=bar2&foo1=bar1
First, we'll decode the URL and Query String encoded with [uri]::EscapeDataString() in the above example:
$url =
'https://example.vertigion.com/foos?foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar''%22&complex%3
B%2F%3F%3A%40%26%3D%2B%24%2C%20foo''%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
This gives you back [hashtable]$url_parts; which equals (Note: the spaces in the complex parts are spaces):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString
foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar'%22&complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo'%
22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
Now, we'll decode the URL and Query String encoded with [System.Web.HttpUtility]::UrlEncode() in the above
example:
https://example.vertigion.com/foos?
$url =
'https://example.vertigion.com/foos?foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b
%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
This gives you back [hashtable]$url_parts, which equals (Note: the spaces in the complex parts are plus signs (+)
in the first part and spaces in the second part):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString
foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%
22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
First, we'll decode the URL and Query String encoded with [uri]::EscapeDataString() in the above example:
$url =
'https://example.vertigion.com/foos?foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar''%22&complex%3
B%2F%3F%3A%40%26%3D%2B%24%2C%20foo''%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
This gives you back [hashtable]$url_parts; which equals (Note: the spaces in the complex parts are spaces):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString
foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar'%22&complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo'%
22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
Now, we'll decode the URL and Query String encoded with [System.Web.HttpUtility]::UrlEncode() in the above
example:
https://example.vertigion.com/foos?
$url =
'https://example.vertigion.com/foos?foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b
%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
This gives you back [hashtable]$url_parts; which equals (Note: the spaces in the complex parts are spaces):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString
foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%
22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
Terminating error
Non-Terminating error
As the name says Terminating errors will terminate the execution and a Non-Terminating Errors let the execution
continue to next statement.
Terminating error
Try
{
Write-Host "Attempting Divide By Zero"
1/0
}
Catch
{
Write-Host "A Terminating Error: Divide by Zero Caught!"
}
The above snippet will execute and the error will be caught thru the catch block.
Non-Terminating Error
A Non-Terminating error in the other hand will not be caught in the catch block by default. The reason behind that
is a Non-Terminating error is not considered a critical error.
Try
{
Stop-Process -Id 123456
}
Catch
{
Write-Host "Non-Terminating Error: Invalid Process ID"
}
If you execute the above the line you won't get the output from catch block as since the error is not considered
critical and the execution will simply continue from next command. However, the error will be displayed in the
console. To handle a Non-Terminating error, you simple have to change the error preference.
Try
{
Now, with the updated Error preference, this error will be considered a Terminating error and will be caught in the
catch block.
Write-Error cmdlet simply writes the error to the invoking host program. It doesn't stop the execution. Where as
throw will give you a terminating error and stop the execution
PowerShellGallery.com is the default source for PowerShell modules. You can also browse the site for available
packages, command and preview the code.
Register-PSRepository -Default
# Stream setup
$stream = $data.GetStream()
$bytes = New-Object System.Byte[] 1024
Start listening with the following and capture any message in the variable $msg:
Send-TCPMessage -Port 29800 -Endpoint 192.168.0.1 -message "My first TCP message !"
Note: TCP messages may be blocked by your software firewall or any external facing firewalls you are trying to go
through. Ensure that the TCP port you set in the above command is open and that you are have setup the listener
on the same port.
Several unique "out of box" features are included with the Workflow engine, most notably, job persistence.
workflow DoSomeWork {
param (
[string[]] $ComputerName
)
Get-Process -ComputerName $ComputerName
}
workflow DoSomeWork {
Get-Process -ComputerName server01
Get-Process -ComputerName server02
Get-Process -ComputerName server03
}
DoSomeWork -AsJob
Calling workflow activities in parallel may help to improve performance of your workflow.
This topic is to briefly describe how C# or VB .NET Managed code can be scripted and utilised within a PowerShell
script. This topic is not exploring all facets of the Add-Type cmdlet.
For more information on the Add-Type cmdlet, please refer to the MSDN documentation (for 5.1) here:
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/add-type
$code = "
using System;
namespace MyNameSpace
{
public class Responder
{
public static void StaticRespond()
{
Console.WriteLine("Static Response");
}
# Check the type has not been previously added within the session, otherwise an exception is raised
if (-not ([System.Management.Automation.PSTypeName]'MyNameSpace.Responder').Type)
{
Add-Type -TypeDefinition $code -Language CSharp;
}
[MyNameSpace.Responder]::StaticRespond();
$code = @"
Namespace MyNameSpace
Public Class Responder
Public Shared Sub StaticRespond()
Console.WriteLine("Static Response")
End Sub
# Check the type has not been previously added within the session, otherwise an exception is raised
if (-not ([System.Management.Automation.PSTypeName]'MyNameSpace.Responder').Type)
{
Add-Type -TypeDefinition $code -Language VisualBasic;
}
[MyNameSpace.Responder]::StaticRespond();
.SYNOPSIS
Gets the content of an INI file.
.DESCRIPTION
Gets the content of an INI file and returns it as a hashtable.
.INPUTS
System.String
.OUTPUTS
System.Collections.Hashtable
.PARAMETER FilePath
Specifies the path to the input INI file.
.EXAMPLE
C:\PS>$IniContent = Get-IniContent -FilePath file.ini
C:\PS>$IniContent['Section1'].Key1
Gets the content of file.ini and access Key1 from Section1.
.LINK
Out-IniFile
#>
function Get-IniContent
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]
[System.String]$FilePath
)
return $ini
}
The above function documentation can be displayed by running Get-Help -Name Get-IniContent -Full:
.SYNOPSIS
Reads a CSV file and filters it.
.DESCRIPTION
The ReadUsersCsv.ps1 script reads a CSV file and filters it on the 'UserName' column.
.INPUTS
None. You cannot pipe objects to ReadUsersCsv.ps1.
.OUTPUTS
None. ReadUsersCsv.ps1 does not generate any output.
.EXAMPLE
C:\PS> .\ReadUsersCsv.ps1 -Path C:\Temp\Users.csv -UserName j.doe
#>
Param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$false)]
[System.String]
$Path,
[Parameter(Mandatory=$true,ValueFromPipeline=$false)]
[System.String]
$UserName
)
The above script documentation can be displayed by running Get-Help -Name ReadUsersCsv.ps1 -Full:
The Archive module Microsoft.PowerShell.Archive provides functions for storing files in ZIP archives (Compress-
Archive) and extracting them (Expand-Archive). This module is available in PowerShell 5.0 and above.
This command:
this will add or replace all files Documents.zip with the new ones from C:\Documents
this will extract all files from Documents.zip into the folder C:\Documents
The tested application read and sum every new line and will provide the result after a single white line is provided.
The power shell script write "pass" when the output match.
DSC rules
This will analyze your script against every built-in rule. If your script is sufficiently large that could result in a lot of
warnings and/or errors.
To run the script analyzer against a whole directory, specify the folder containing the script, module and DSC files
you want analyzed. Specify the Recurse parameter if you also want sub-directories searched for files to analyze.
If you run this configuration in Powershell (EnableIISFeature), it will produce a localhost.mof file. This is the
"compiled" configuration you can run on a machine.
To test the DSC configuration on your localhost, you can simply invoke the following:
$remoteComputer = "myserver.somedomain.com"
$cred = (Get-Credential)
Start-DSCConfiguration -ServerName $remoteComputer -Credential $cred -Verbose
Nb: Assuming you have compiled a configuration for your node on your localmachine (and that the file
myserver.somedomain.com.mof is present prior to starting the configuration)
Powershell 5 (WMF5) added this neat little feature for doing this called Import-PowerShellDataFile .
Example:
Get-DscResource
This will list all resources for all installed modules (that are in your PSModulePath) on your authoring node.
Find-DSCResource
Example showing how to import the PSDesiredStateConfiguration resource and the File resource.
Configuration InstallPreReqs
{
param(); # params to DSC goes here.
Import-DscResource PSDesiredStateConfiguration
File CheckForTmpFolder {
Type = 'Directory'
DestinationPath = 'C:\Tmp'
Ensure = "Present"
}
}
Note: In order for DSC Resources to work, you must have the modules installed on the target machines when
running the configuration. If you don't have them installed, the configuration will fail.
This example also shows that underlying commands will also listen to the -Confirm flag!
<#
Restart-Win32Computer
#>
function Restart-Win32Computer
{
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$computerName,
[parameter(Mandatory=$true)]
[string][ValidateSet("Restart","LogOff","Shutdown","PowerOff")] $action,
[boolean]$force = $false
)
BEGIN {
# translate action to numeric value required by the method
switch($action) {
"Restart"
{
$_action = 2
break
}
"LogOff"
{
$_action = 0
break
}
"Shutdown"
{
$_action = 2
break
}
"PowerOff"
{
$_action = 8
break
}
}
# to force, add 4 to the value
if($force)
{
$_action += 4
}
write-verbose "Action set to $action"
}
PROCESS {
write-verbose "Attempting to connect to $computername"
#This will request the permission of the caller to continue with this item.
#Attention: in this example you will get two confirmation request because all cmdlets called by
this cmdlet that also support ShouldProcess, will ask for their own confirmations...
'localhost','server1'| Restart-Win32Computer -action LogOff -Confirm
Are you sure you want to perform this action? Performing operation "Invoke-MyCmdlet" on target "Target of action"
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
function Get-Sum
{
foreach ($i in $Input)
{$Sum += $i}
$Sum
1..10 | Get-Sum
#output
55
To aid in building DSC Resource, there's a [DscResource()] attribute that's applied to the class definition, and a
[DscProperty()] resource to designate properties as configurable by the DSC Resource user.
This example demonstrates how to build the outer section of a PowerShell class, that declares a DSC Resource. You
still need to fill in the contents of the class definition.
A DSC Resource must declare at least one key property. The key property is what uniquely identifies the resource
from other resources. For example, let's say that you're building a DSC Resource that represents a ticket in a
ticketing system. Each ticket would be uniquely represented with a ticket ID.
Each property that will be exposed to the user of the DSC Resource must be decorated with the [DscProperty()]
attribute. This attributes accepts a key parameter, to indicate that the property is a key attribute for the DSC
Resource.
[DscProperty(Mandatory)]
[string] $Subject
}
When building a DSC Resource, you'll often find that not every single property should be mandatory. However,
there are some core properties that you'll want to ensure are configured by the user of the DSC Resource. You use
the Mandatory parameter of the [DscResource()] attribute to declare a property as required by the DSC Resource's
user.
In the example above, we've added a Subject property to a Ticket resource, that represents a unique ticket in a
ticketing system, and designated it as a Mandatory property.
[void] Set() {
# Create or update the resource
}
[Ticket] Get() {
# Return the resource's current state as an object
$TicketState = [Ticket]::new()
return $TicketState
}
[bool] Test() {
# Return $true if desired state is met
# Return $false if desired state is not met
return $false
}
}
This is a complete DSC Resource that demonstrates all of the core requirements to build a valid resource. The
method implementations are not complete, but are provided with the intention of showing the basic structure.
Version ≥ 3.0
CIM:
WMI:
Using a filter
You can apply a filter to only get specific instances of a CIM/WMI-class. Filters are written using WQL (default) or CQL
(add -QueryDialect CQL). -Filter uses the WHERE-part of a full WQL/CQL-query.
Version ≥ 3.0
CIM:
WMI:
...
Caption : powershell.exe
CommandLine : "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
CreationClassName : Win32_Process
CreationDate : 20160913184324.393887+120
CSCreationClassName : Win32_ComputerSystem
CSName : STACKOVERFLOW-PC
Description : powershell.exe
Using a WQL-query:
Version ≥ 3.0
CIM:
Version ≥ 3.0
CIM:
WMI:
__GENUS : 2
__CLASS : AntiVirusProduct
__SUPERCLASS :
__DYNASTY : AntiVirusProduct
__RELPATH : AntiVirusProduct.instanceGuid="{D68DDC3A-831F-4fae-9E44-DA132C1ACF46}"
__PROPERTY_COUNT : 6
__DERIVATION : {}
__SERVER : STACKOVERFLOW-PC
__NAMESPACE : ROOT\SecurityCenter2
__PATH : \\STACKOVERFLOW-
PC\ROOT\SecurityCenter2:AntiVirusProduct.instanceGuid="{D68DDC3A-831F-4fae-9E44-DA132C1ACF46}"
displayName : Windows Defender
instanceGuid : {D68DDC3A-831F-4fae-9E44-DA132C1ACF46}
pathToSignedProductExe : %ProgramFiles%\Windows Defender\MSASCui.exe
pathToSignedReportingExe : %ProgramFiles%\Windows Defender\MsMpeng.exe
productState : 397568
timestamp : Fri, 09 Sep 2016 21:26:41 GMT
PSComputerName : STACKOVERFLOW-PC
You can list all available classes in the default namespace (root/cimv2) on a computer.
Version ≥ 3.0
CIM:
Get-CimClass
WMI:
Get-WmiObject -List
You can search for specific classes using wildcards. Ex: Find classes containing the word process.
Version ≥ 3.0
CIM:
NameSpace: ROOT/CIMV2
WMI:
The root namespace is simply called root. You can list classes in another namespace using the -NameSpace
Version ≥ 3.0
CIM:
NameSpace: ROOT/SecurityCenter2
WMI:
To find available child-namespaces of root (or another namespace), query the objects in the __NAMESPACE-class for
that namespace.
Version ≥ 3.0
CIM:
Name PSComputerName
---- --------------
subscription
DEFAULT
CIMV2
msdtc
Cli
SECURITY
HyperVCluster
SecurityCenter2
RSOP
PEH
StandardCimv2
WMI
directory
Policy
virtualization
Interop
Hardware
ServiceModel
SecurityCenter
Microsoft
aspnet
Appv
New AD User
New-ADUser -Name "MarySmith" -GivenName "Mary" -Surname "Smith" -DisplayName "MarySmith" -Path
"CN=Users,DC=Domain,DC=Local"
Get-ADGroup -Identity "My-First-Group" #Ensure if group name has space quotes are used
New AD Group
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
This only works in the 64bit version of PowerShell. If the window says "Windows PowerShell (x86)" in the title
you are using the incorrect version.
If the Snap-In is already loaded, the code above will cause an error. Using the following will load only if necessary,
which can be used in Cmdlets/functions:
Alternatively, if you start the SharePoint Management Shell, it will automatically include the Snap-In.
To get a list of all the available SharePoint Cmdlets, run the following:
Get-SPFeature can also be run on web scope (-Web <WebUrl>), farm scope (-Farm) and web application scope (-
WebApplication <WebAppUrl>).
Another usage of Get-SPFeature can be to find all features that have no scope:
Task Build {
"Build"
}
Task Clean {
"Clean"
}
Task Build {
"Build"
}
Task Clean {
"Clean"
}
# By default the Build task won't run, unless there is a param $true
Task Build -precondition { return $isOk } {
"Build"
}
Describe: Defines a group of tests. All Pester test files needs at least one Describe-block.
It: Defines an individual test. You can have multiple It-blocks inside a Describe-block.
Should: The verify/test command. It is used to define the result that should be considered a successful test.
Sample:
Import-Module Pester
#Group of tests
Describe "Validate Add-Numbers" {
Output:
$credential = Get-Credential
$plainPass = $credential.GetNetworkCredential().Password
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secStr)
$plainPass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
$credential = Get-Credential
$credential = Get-Credential -Message 'Please enter your company email address and password.'
$credential = Get-Credential
To re-import:
The important thing to remember is that by default this uses the Windows data protection API, and the key used to
encrypt the password is specific to both the user and the machine that the code is running under.
As a result, the encrypted credential cannot be imported by a different user nor the same user on a
By encrypting several versions of the same credential with different running users and on different computers, you
can have the same secret available to multiple users.
By putting the user and computer name in the file name, you can store all of the encrypted secrets in a way that
allows for the same code to use them without hard coding anything:
Encrypter
# run as each user, and on each computer
$credential = Get-Credential
The correct version of the file for the running user will be loaded automatically (or it will fail because the file doesn't
exist).
$username = "[email protected]"
$AESKey = Get-Content $AESKeyFilePath
$pwdTxt = Get-Content $SecurePwdFilePath
$securePwd = $pwdTxt | ConvertTo-SecureString -Key $AESKey
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username,
$securePwd
# Now, $credObject is having the credentials stored with AES Key and you can pass it wherever you
want.
#calculate
$hash=[System.Security.Cryptography.HashAlgorithm]::Create("sha256").ComputeHash(
[System.Text.Encoding]::UTF8.GetBytes($example))
#convert to hex
[System.BitConverter]::ToString($hash)
#2E-DF-DA-DA-56-52-5B-12-90-FF-16-FB-17-44-CF-B4-82-DD-29-14-FF-BC-B6-49-79-0C-0E-58-9E-46-2D-3D
#2edfdada56525b1290ff16fb1744cfb482dd2914ffbcb649790c0e589e462d3d
#convert to base64
[Convert]::ToBase64String($hash)
#Lt/a2lZSWxKQ/xb7F0TPtILdKRT/vLZJeQwOWJ5GLT0=
#Get the first available personal code-signing certificate for the logged on user
$cert = @(Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert)[0]
The script will be valid until the certificate expires. If you use a timestamp-server during the signing, the script will
continue to be valid after the certificate expires. It is also useful to add the trust chain for the certificate (including
root authority) to help most computers trust the certificated used to sign the script.
It's recommended to use a timestamp-server from a trusted certificate provider like Verisign, Comodo, Thawte etc.
Unrestricted* Caveat: If you run an unsigned script that was downloaded from the Internet, you are prompted for
permission before it runs.
Set-ExecutionPolicy AllSigned
PS> Get-ExecutionPolicy
RemoteSigned
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned
Version ≥ 5.0
Beginning with PowerShell 5.0 you can generate a self-signed code signing certificate by using the New-
SelfSignedCertificate-cmdlet:
In earlier versions, you can create a self-signed certificate using the makecert.exe tool found in the .NET Framework
SDK and Windows SDK.
A self-signed certificate will only be trusted by computers that have installed the certificate. For scripts that will be
shared, a certificate from a trusted certificate authority (internal or trusted third-party) are recommended.
# Replace matching IPv4 from sourcefile and creating a temp file IPV4.txt
Get-Content $SourceFile | Foreach-Object {$_ -replace $Regex_v4, $Anonym_v4} | Set-Content
$IPv4File
# Replace matching IPv6 from IPV4.txt and creating a temp file ANONYM.txt
Get-Content $IPv4File | Foreach-Object {$_ -replace $Regex_v6, $Anonym_v6} | Set-Content $DestFile
$REKResult.Labels
After running the script above, you should have results printed in your PowerShell host that look something similar
to the following:
RESULTS:
Confidence Name
---------- ----
86.87605 Indoors
86.87605 Interior Design
86.87605 Room
77.4853 Kitchen
77.25354 Housing
77.25354 Loft
66.77325 Appliance
66.77325 Oven
Using the AWS PowerShell module in conjunction with the AWS Rekognition service, you can detect labels in an
image, such as identifying objects in a room, attributes about photos you took, and the corresponding confidence
level that AWS Rekognition has for each of those attributes.
The Find-REKLabel command is the one that enables you to invoke a search for these attributes / labels. While you
can provide image content as a byte array during the API call, a better method is to upload your image files to an
AWS S3 Bucket, and then point the Rekognition service over to the S3 Objects that you want to analyze. The
example above shows how to accomplish this.
The example script provided above should give you results similar to the following:
Face Similarity
---- ----------
Amazon.Rekognition.Model.ComparedFace 90
The AWS Rekognition service enables you to perform a facial comparison between two photos. Using this service is
quite straightforward. Simply upload two image files, that you want to compare, to an AWS S3 Bucket. Then, invoke
the Compare-REKFace command, similar to the example provided above. Of course, you'll need to provide your own,
globally-unique S3 Bucket name and file names.
This documentation section focuses on developing against the Amazon Web Services (AWS) Simple Storage Service
(S3). S3 is truly a simple service to interact with. You create S3 "buckets" which can contain zero or more "objects."
Once you create a bucket, you can upload files or arbitrary data into the S3 bucket as an "object." You reference S3
objects, inside of a bucket, by the object's "key" (name).
The Simple Storage Service (S3) bucket name must be globally unique. This means that if someone else has already
used the bucket name that you want to use, then you must decide on a new name.
Uploading files from your local filesystem into AWS S3 is easy, using the Write-S3Object command. In its most
basic form, you only need to specify the -BucketName parameter, to indicate which S3 bucket you want to upload a
file into, and the -File parameter, which indicates the relative or absolute path to the local file that you want to
upload into the S3 bucket.
In order to remove a S3 bucket, you must first remove all of the S3 objects that are stored inside of the bucket,
provided you have permission to do so. In the above example, we are retrieving a list of all the objects inside a
bucket, and then piping them into the Remove-S3Object command to delete them. Once all of the objects have
been removed, we can use the Remove-S3Bucket command to delete the bucket.
Adam M. Chapter 23
ajb101 Chapter 51
Alban Chapter 25
Andrei Epure Chapter 28
ANIL Chapter 52
Anthony Neace Chapters 3 and 8
AP. Chapter 69
Austin French Chapter 17
autosvet Chapters 1, 2 and 20
Avshalom Chapter 24
Bert Levrau Chapters 12, 27 and 43
boeprox Chapter 13
Brant Bobby Chapters 1, 58, 13 and 19
briantist Chapters 17 and 67
camilohe Chapter 27
Chris N Chapters 1 and 11
Christophe Chapter 53
Christopher G. Lewis Chapter 7
Clijsters Chapters 1, 3 and 26
CmdrTchort Chapters 7 and 57
DarkLite1 Chapters 1 and 22
Dave Anderson Chapter 22
DAXaholic Chapters 1 and 7
Deptor Chapter 25
djwork Chapter 11
Eris Chapters 7, 2 and 27
Euro Micelli Chapter 5
Florian Meyer Chapters 10 and 60
FoxDeploy Chapter 1
Frode F. Chapters 7, 13, 8, 29, 9, 15, 38, 39, 21, 69, 28, 66, 40, 35, 32 and 62
Giorgio Gambino Chapter 29
Giulio Caccin Chapter 55
Gordon Bell Chapters 1 and 3
Greg Bray Chapter 1
HAL9256 Chapter 30
It Chapter 1
James Ruskin Chapters 12, 25 and 54
Jaqueline Vanek Chapter 13
jimmyb Chapter 23
JNYRanger Chapter 1
JPBlanc Chapter 3
jumbo Chapters 7, 8, 27, 34, 33, 19, 38 and 42
Keith Chapter 25
Kolob Canyon Chapter 15
Lachie White Chapter 63
Liam Chapters 2 and 6
Lieven Keersmaekers Chapter 29
lloyd Chapter 6
Luke Ryan Chapter 12