PowerShell Scripting
Cmdlets are formed by a verb-noun pair. Cmdlets are case-insensitive.
It's all .NET A PS string is in fact a .NET System.String All .NET methods and properties are thus available
Note that .NET functions MUST be called with parentheses while PS functions CANNOT be called with parentheses. If you do call a cmdlet/PS function with parentheses, it is the same as passing a single parameter list.
Screen Output
PowerShell | |
---|---|
User Input
Variables
Built-in Variables
Lists & Dictionaries
Flow Control
PowerShell | |
---|---|
Switch
Switch
has the following parameters:
- Wildcard: Indicates that the condition is a wildcard string. If the match clause is not a string, the parameter is ignored. The comparison is case-insensitive.
- Exact: Indicates that the match clause, if it is a string, must match exactly. If the match clause is not a string, this parameter is ignored. The comparison is case-insensitive.
- CaseSensitive: Performs a case-sensitive match. If the match clause is not a string, this parameter is ignored.
- File: Takes input from a file rather than a value statement. If multiple File parameters are included, only the last one is used. Each line of the file is read and evaluated by the Switch statement. The comparison is case-insensitive.
- Regex: Performs regular expression matching of the value to the condition. If the match clause is not a string, this parameter is ignored. The comparison is case-insensitive. The
$matches
automatic variable is available for use within the matching statement block.
Loops
PowerShell | |
---|---|
Operators
PowerShell | |
---|---|
Exception Handling
PowerShell | |
---|---|
Functions
If the function defines a Begin
, Process
or End
block, all the code must reside inside those blocks. No code will be recognized outside the blocks if any of the blocks are defined.
If the function has a Process
keyword, each object in $input
is removed from $input
and assigned to $_
.
Optionally, it's possible to provide a brief help string that describes the default value of the parameter, by adding the PSDefaultValue
attribute to the description of the parameter, and specifying the Help
property of PSDefaultValue
.
Script Arguments
Parsing Script Arguments
PowerShell | |
---|---|
Script Named Arguments
In scripts.ps1
:
PowerShell | |
---|---|
In PowerShell:
PowerShell | |
---|---|
Filters
A filter is a type of function that runs on each object in the pipeline. A filter resembles a function with all its statements in a Process
block.
PowerShell | |
---|---|
PowerShell Comment-based Help
The syntax for comment-based help is as follows:
or
Comment-based help is written as a series of comments. You can type a comment symbol #
before each line of comments, or you can use the <#
and #>
symbols to create a comment block. All the lines within the comment block are interpreted as comments.
All of the lines in a comment-based help topic must be contiguous. If a comment-based help topic follows a comment that is not part of the help topic, there must be at least one blank line between the last non-help comment line and the beginning of the comment-based help.
Keywords define each section of comment-based help. Each comment-based help keyword is preceded by a dot .
. The keywords can appear in any order. The keyword names are not case-sensitive.
.SYNOPSIS
A brief description of the function or script. This keyword can be used only once in each topic.
.DESCRIPTION
A detailed description of the function or script. This keyword can be used only once in each topic.
.PARAMETER
The description of a parameter. Add a .PARAMETER
keyword for each parameter in the function or script syntax.
Type the parameter name on the same line as the .PARAMETER
keyword. Type the parameter description on the lines following the .PARAMETER
keyword. Windows PowerShell interprets all text between the .PARAMETER
line and the next keyword or the end of the comment block as part of the parameter description. The description can include paragraph breaks.
PowerShell | |
---|---|
The Parameter keywords can appear in any order in the comment block, but the function or script syntax determines the order in which the parameters (and their descriptions) appear in help topic. To change the order, change the syntax.
You can also specify a parameter description by placing a comment in the function or script syntax immediately before the parameter variable name. For this to work, you must also have a comment block with at least one keyword.
If you use both a syntax comment and a .PARAMETER
keyword, the description associated with the .PARAMETER
keyword is used, and the syntax comment is ignored.
PowerShell | |
---|---|
.EXAMPLE
A sample command that uses the function or script, optionally followed by sample output and a description. Repeat this keyword for each example.
.INPUTS
The .NET types of objects that can be piped to the function or script. You can also include a description of the input objects.
.OUTPUTS
The .NET type of the objects that the cmdlet returns. You can also include a description of the returned objects.
.NOTES
Additional information about the function or script.
.LINK
The name of a related topic. The value appears on the line below the .LINK
keyword and must be preceded by a comment symbol #
or included in the comment block.
Repeat the .LINK
keyword for each related topic.
This content appears in the Related Links section of the help topic.
The .Link
keyword content can also include a Uniform Resource Identifier (URI) to an online version of the same help topic. The online version opens when you use the Online parameter of Get-Help
. The URI must begin with "http" or "https".
.COMPONENT
The name of the technology or feature that the function or script uses, or to which it is related. The Component parameter of Get-Help
uses this value to filter the search results returned by Get-Help
.
.ROLE
The name of the user role for the help topic. The Role parameter of Get-Help
uses this value to filter the search results returned by Get-Help
.
.FUNCTIONALITY
The keywords that describe the intended use of the function. The Functionality parameter of Get-Help
uses this value to filter the search results returned by Get-Help
.
.FORWARDHELPTARGETNAME
Redirects to the help topic for the specified command. You can redirect users to any help topic, including help topics for a function, script, cmdlet, or provider.
PowerShell | |
---|---|
.FORWARDHELPCATEGORY
Specifies the help category of the item in .ForwardHelpTargetName
. Valid values are Alias
, Cmdlet
, HelpFile
, Function
, Provider
, General
, FAQ
, Glossary
, ScriptCommand
, ExternalScript
, Filter
, or All
. Use this keyword to avoid conflicts when there are commands with the same name.
PowerShell | |
---|---|
.REMOTEHELPRUNSPACE
Specifies a session that contains the help topic. Enter a variable that contains a PSSession object. This keyword is used by the Export-PSSession cmdlet to find the help topics for the exported commands.
PowerShell | |
---|---|
.EXTERNALHELP
Specifies an XML-based help file for the script or function.
PowerShell | |
---|---|
The .ExternalHelp
keyword is required when a function or script is documented in XML files. Without this keyword, Get-Help
cannot find the XML-based help file for the function or script.
The .ExternalHelp
keyword takes precedence over other comment-based help keywords. If .ExternalHelp
is present, Get-Help
does not display comment-based help, even if it cannot find a help topic that matches the value of the .ExternalHelp
keyword.
If the function is exported by a module, set the value of the .ExternalHelp
keyword to a filename without a path. Get-Help
looks for the specified file name in a language-specific subdirectory of the module directory. There are no requirements for the name of the XML-based help file for a function, but a best practice is to use the following format:
PowerShell | |
---|---|
If the function is not included in a module, include a path to the XML-based help file. If the value includes a path and the path contains UI-culture-specific subdirectories, Get-Help
searches the subdirectories
recursively for an XML file with the name of the script or function in accordance with the language fallback standards established for Windows, just as it does in a module directory.
For more information about the cmdlet help XML-based help file format, see How to Write Cmdlet Help in the MSDN library.