TOPIC
about_Language_Keywords
SHORT DESCRIPTION
Describes the keywords in the Windows PowerShell scripting language.
LONG DESCRIPTION
Windows PowerShell has the following language keywords. For more
information, see the about topic for the keyword and the information that
follows the table.
Keyword Reference
------- ---------
Begin about_Functions, about_Functions_Advanced
Break about_Break, about_Trap
Catch about_Try_Catch_Finally
Continue about_Continue, about_Trap
Data about_Data_Sections
Do about_Do, about_While
Dynamicparam about_Functions_Advanced_Parameters
Else about_If
Elseif about_If
End about_Functions, about_Functions_Advanced_Methods
Exit Described in this topic.
Filter about_Functions
Finally about_Try_Catch_Finally
For about_For
Foreach about_Foreach
From Reserved for future use.
Function about_Functions, about_Functions_Advanced
If about_If
In about_Foreach
Param about_Functions
Process about_Functions, about_Functions_Advanced
Return about_Return
Switch about_Switch
Throw about_Throw, about_Functions_Advanced_Methods
Trap about_Trap, about_Break, about_Try_Catch_Finally
Try about_Try_Catch_Finally
Until about_Do
While about_While, about_Do
Language Keywords
Begin
-----
Specifies one part of the body of a function, along with the
Dynamicparam, Process, and End keywords. The Begin statement list runs
one time before any objects are received from the pipeline.
Syntax:
function <name> {
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
Break
-----
Causes a script to exit a loop.
Syntax:
while (<condition>) {
<statements>
...
break
...
<statements>
}
Catch
-----
Specifies a statement list to run if an error occurs in the
accompanying Try statement list. An error type requires brackets. The
second pair of brackets indicates that the error type is optional.
Syntax:
try {<statement list>}
catch [[<error type>]] {<statement list>}
Continue
--------
Causes a script to stop running a loop and to go back to the condition.
If the condition is met, the script begins the loop again.
Syntax:
while (<condition>) {
<statements>
...
continue
...
<statements>
}
Data
----
In a script, defines a section that isolates data from the script logic.
Can also include If statements and some limited commands.
Syntax:
data <variable> [-supportedCommand <cmdlet-name>] {<permitted content>}
Do
--
Used with the While or Until keyword as a looping construct. Windows
PowerShell runs the statement list at least one time, unlike a loop that
uses While.
Syntax:
do {<statement list>} while (<condition>)
do {<statement list>} until (<condition>)
Dynamicparam
------------
Specifies one part of the body of a function, along with the Begin,
Process, and End keywords. Dynamic parameters are added at run time.
Syntax:
function <name> {
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
Else
----
Used with the If keyword to specify the default statement list.
Syntax:
if (<condition>) {<statement list>}
else {<statement list>}
Elseif
------
Used with the If and Else keywords to specify additional conditionals.
The Else keyword is optional.
Syntax:
if (<condition>) {<statement list>}
elseif (<condition>) {<statement list>}
else {<statement list>}
End
---
Specifies one part of the body of a function, along with the
Dynamicparam, Begin, and End keywords. The End statement list runs one
time after all the objects have been received from the pipeline.
Syntax:
function <name> {
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
Exit
----
Causes Windows PowerShell to exit a script or a Windows PowerShell
instance.
Syntax:
exit
Filter
------
Specifies a function in which the statement list runs one time for each
input object. It has the same effect as a function that contains only a
Process block.
Syntax:
filter <name> {<statement list>}
Finally
-------
Defines a statement list that runs after statements that are associated
with Try and Catch. A Finally statement list runs even if you press
CTRL+C to leave a script or if you use the Exit keyword in the script.
Syntax:
try {<statement list>}
catch [<error type] {<statement list>}
finally {<statement list>}
For
---
Defines a loop by using a condition.
Syntax:
for (<initialize>; <condition>; <iterate>) {<statement list>}
Foreach
-------
Defines a loop by using each member of a collection.
Syntax:
foreach (<item> in <collection>){<statement list>}
From
-----
Reserved for future use.
Function
--------
Creates a named statement list of reusable code. You can name the scope a
function belongs to. And, you can specify one or more named parameters by
using the Param keyword. Within the function statement list, you can
include Dynamicparam, Begin, Process, and End statement lists.
Syntax:
function [<scope:>]<name> {
param ([type]<$pname1> [, [type]<$pname2>])
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
You also have the option of defining one or more parameters outside the
statement list after the function name.
Syntax:
function [<scope:>]<name> [([type]<$pname1>, [[type]<$pname2>])] {
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
If
--
Defines a conditional.
Syntax:
if (<condition>) {<statement list>}
In
--
Used in a Foreach statement to create a loop that uses each member of a
collection.
Syntax:
foreach (<item> in <collection>){<statement list>}
Param
-----
Defines the parameters in a function.
Syntax:
function [<scope:>]<name> {
param ([type]<$pname1>[, [[type]<$pname2>]])
<statement list>
}
Process
-------
Specifies a part of the body of a function, along with the Dynamicparam,
Begin, and End keywords. When a Process statement list receives input
from the pipeline, the Process statement list runs one time for each
element from the pipeline. If the pipeline provides no objects, the
Process statement list does not run. If the command is the first command
in the pipeline, the Process statement list runs one time.
Syntax:
function <name> {
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
Return
------
Causes Windows PowerShell to leave the current scope, such as a script or
function, and writes the optional expression to the output.
Syntax:
return [<expression>]
Switch
------
Specifies a variety of actions to be performed on items from the pipeline
or from a file. You can use either of the following syntax models.
Syntax 1:
switch [-regex|-wildcard|-exact][-casesensitive] ( pipeline )
{
<string>|<number>|<variable>|{ <expression> } {<statement list>}
<string>|<number>|<variable>|{ <expression> } {<statement list>}
...
default {<statement list>}
}
Syntax 2:
switch [-regex|-wildcard|-exact][-casesensitive] -file filename
{
<string>|<number>|<variable>|{ <expression> } {<statement list>}
<string>|<number>|<variable>|{ <expression> } {<statement list>}
...
default {<statement list>}
}
Throw
-----
Throws an object as an error.
Syntax:
throw [<object>]
Trap
----
Defines a statement list to be run if an error is encountered. An error
type requires brackets. The second pair of brackets indicates that the
error type is optional.
Syntax:
trap [[<error type>]] {<statement list>}
Try
---
Defines a statement list to be checked for errors while the statements
run. If an error occurs, Windows PowerShell continues running in a Catch
or Finally statement. An error type requires brackets. The second pair of
brackets indicates that the error type is optional.
Syntax:
try {<statement list>}
catch [[<error type]] {<statement list>}
finally {<statement list>}
Until
-----
Used in a Do statement as a looping construct where the statement list is
executed at least one time.
Syntax:
do {<statement list>} until (<condition>)
While
-----
Used in a Do statement as a looping construct where the statement list is
executed at least one time.
Syntax:
do {<statement list>} while (<condition>)
SEE ALSO
about_Escape_Characters
about_Reserved_Words
about_Special_Characters
about_Wildcards
|