PowerShell Language Keywords

In this tutorial we will see about PowerShell Language Keywords. PowerShell Keywords are used in order to perform a specific action. For this tutorial we will see the list of keywords and the syntax of each one. In related links section you will be able to find a link for each of the keywords in order to check more information about them. I will write a different post for each one of them, in order to see in detail the use of them. Consider this as an introduction to PoowerShell Keywords.

[adinserter name=”In Article”]

Keyword List

Begin – It specifies one part of the body of a function and runs one time before any objects are received from the pipeline.

– It specifies one part of the body of a function and runs one time before any objects are received from the pipeline. Break – It causes a script to exit a loop.

– It causes a script to exit a loop. Catch – It specifies a statement list to run if an error occurs in the accompanying Try statement list.

– It specifies a statement list to run if an error occurs in the accompanying Try statement list. Class – It specifies a new class in PowerShell.

– It specifies a new class in PowerShell. Continue – It 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.

– It 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. Data – It defines a section that isolates data from the script logic.

– It defines a section that isolates data from the script logic. Define – Reserved for future use

– Reserved for future use Do – It is used with the While or Until keyword as a looping construct.

– It is used with the While or Until keyword as a looping construct. DynamicParam – It specifies one part of the body of a function. Dynamic parameters are added at run time.

– It specifies one part of the body of a function. Dynamic parameters are added at run time. Else – It is used with the If keyword to specify the default statement list.

– It is used with the If keyword to specify the default statement list. Elseif – It is used with the If and Else keywords to specify additional conditionals. The Else keyword is optional.

– It is used with the If and Else keywords to specify additional conditionals. The Else keyword is optional. End – It specifies one part of the body of a function and runs one time after all the objects have been received from the pipeline.

– It specifies one part of the body of a function and runs one time after all the objects have been received from the pipeline. Enum – It is used to declare an enumeration, a distinct type that consists of a set of named labels called the enumerator list.

– It is used to declare an enumeration, a distinct type that consists of a set of named labels called the enumerator list. Exit – It causes PowerShell to exit a script or a PowerShell instance.

– It causes PowerShell to exit a script or a PowerShell instance. Filter – It 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.

– It 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. Finally – It defines a statement list that runs after statements that are associated with Try and Catch.

– It defines a statement list that runs after statements that are associated with Try and Catch. For – It defines a loop by using a condition.

– It defines a loop by using a condition. ForEach – It defines a loop by using each member of a collection.

– It defines a loop by using each member of a collection. From – Reserved for future use.

– Reserved for future use. Function – It creates a named statement list of reusable code.

– It creates a named statement list of reusable code. Hidden – It hides class members from the default results of the Get-Member cmdlet, and from IntelliSense and tab completion results.

– It hides class members from the default results of the Get-Member cmdlet, and from IntelliSense and tab completion results. If – It defines a conditional.

– It defines a conditional. In – It is used in a ForEach statement to create a loop that uses each member of a collection.

– It is used in a ForEach statement to create a loop that uses each member of a collection. InlineScript – It runs workflow commands in a shared PowerShell session. InlineScript is valid only in a PowerShell Workflow.

– It runs workflow commands in a shared PowerShell session. InlineScript is valid only in a PowerShell Workflow. Param – It defines the parameters in a function.

– It defines the parameters in a function. Process – It specifies a part of the body of a function.

– It specifies a part of the body of a function. Return – It causes PowerShell to leave the current scope and writes the optional expression to the output.

– It causes PowerShell to leave the current scope and writes the optional expression to the output. Static – It specifies the property or method defined is common to all instances of the class in which is defined.

– It specifies the property or method defined is common to all instances of the class in which is defined. Switch – The Switch statement is equivalent to a series of If statements, but it is simpler.

– The Switch statement is equivalent to a series of If statements, but it is simpler. Throw – It throws an object as an error.

– It throws an object as an error. Trap – It defines a statement list to be run if an error is encountered.

– It defines a statement list to be run if an error is encountered. Try – It defines a statement list to be checked for errors while the statements run.

– It defines a statement list to be checked for errors while the statements run. Until – It is used in a Do statement as a looping construct where the statement list is executed at least one time.

– It is used in a Do statement as a looping construct where the statement list is executed at least one time. Using – It allows to indicate which namespaces are used in the session.

– It allows to indicate which namespaces are used in the session. Var – Reserved for future use.

– Reserved for future use. While – It is used in a Do statement as a looping construct where the statement list is executed at least one time.

[adinserter name=”In Article”]

Function – Param – DynamicParam – Begin – Process – End

Syntax:

function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])] { param([type]$parameter1 [,[type]$parameter2]) dynamicparam {<statement list>} begin {<statement list>} process {<statement list>} end {<statement list>} }

Try – Catch – Finally

Syntax:

try {<statement list>} catch [[<error type>][',' <error type>]*] {<statement list>} finally {<statement list>}

Classes – Static

Classes are declared using the following syntax:

class <class-name> [: [<base-class>][,<interface-list]] { [[<attribute>] [hidden] [static] <property-definition> ...] [<class-name>([<constructor-argument-list>]) {<constructor-statement-list>} ...] [[<attribute>] [hidden] [static] <method-definition> ...] }

Classes are instantiated using either of the following syntaxes:

[$<variable-name> =] New-Object -TypeName <class-name> [ [-ArgumentList] <constructor-argument-list>]

[$<variable-name> =] [<class-name>]::new([<constructor-argument-list>])

[adinserter name=”In Article”]

Data

Syntax:

DATA [-supportedCommand <cmdlet-name>] { <Permitted content> }

Do-While – Do-Until

Do-While Syntax:

do {<statement list>} while (<condition>)

Do-Until Syntax:

do {<statement list>} until (<condition>)

If – Elseif – Else

Syntax:

if (<test1>) {<statement list 1>} [elseif (<test2>) {<statement list 2>}] [else {<statement list 3>}]

Enum

Basic Syntax:

enum <enum-name> { <label> [= <int-value>] ... }

Flags Syntax:

[Flags()] enum <enum-name> { <label 0> [= 1] <label 1> [= 2] <label 2> [= 4] <label 3> [= 8] ... }

[adinserter name=”In Article”]

Filter

Syntax:

filter [<scope:>]<name> {<statement list>}

For

Syntax:

for (<init>; <condition>; <repeat>) {<statement list>}

ForEach – In

Syntax:

foreach ($<item> in $<collection>){<statement list>}

Return

Syntax:

return [<expression>]

Switch

Syntax:

Switch (<test-value>) { <condition> {<action>} <condition> {<action>} }

[adinserter name=”In Article”]

Throw

Syntax:

throw [<expression>]

Trap

Syntax:

trap [[<error type>]] {<statement list>}

Using

Syntax to reference .Net Framework namespaces:

using namespace <.Net-framework-namespace>

Syntax to reference PowerShell modules:

using module <module-name>

I hope the tutorial about PowerShell Language Keywords is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links

[adinserter name=”Matched-Content”]