about_Language_Keywords

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_Special_Characters
     about_wildcards