PowerShell Reserved Words
In this tutorial we will see the list of PowerShell reserved words. As in every programming / scripting language there are some words that have special meaning. In my opinion, you should avoid using those words for other purposes. This will help you avoid undesired results when you are programming / scripting. In PowerShell there is also such a list and you are able to see it below:
The below list and some explanation is provided from Microsoft:
Already in use
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.
break– Causes a script to exit a loop.
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.
class– Specifies a new class in PowerShell.
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.
data– In a script, defines a section that isolates data from the script logic. Can also include If statements and some limited commands.
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.
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.
else– Used with the If keyword to specify the default statement list.
elseif– Used with the If and Else keywords to specify additional conditionals. The Else keyword is optional.
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.
enum– It is used to declare an enumeration; a distinct type that consists of a set of named labels called the enumerator list.
exit– Causes PowerShell to exit a script or a PowerShell instance.
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.
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.
for– Defines a loop by using a condition.
foreach– Defines a loop by using each member of a collection.
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.
hidden– Hides class members from the default results of the Get-Member cmdlet, and from IntelliSense and tab completion results.
if– Defines a conditional.
in– Used in a ForEach statement to create a loop that uses each member of a collection.
inlinescript– Runs workflow commands in a shared PowerShell session. This keyword is valid only in a PowerShell Workflow.
param– Defines the parameters in a function.
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.
return– Causes PowerShell to leave the current scope, such as a script or function, and writes the optional expression to the output.
static– Specifies the property or method defined is common to all instances of the class in which is defined.
switch– To check multiple conditions, use a Switch statement. The Switch statement is equivalent to a series of If statements, but it is simpler. The Switch statement lists each condition and an optional action. If a condition obtains, the action is performed.
throw– Throws an object as an error.
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.
try– Defines a statement list to be checked for errors while the statements run. If an error occurs, 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.
until– Used in a Do statement as a looping construct where the statement list is executed at least one time.
using– Allows to indicate which namespaces are used in the session. Classes and members require less typing to mention them. You can also include classes from modules.
while– Used in a Do statement as a looping construct where the statement list is executed at least one time.
[adinserter name=”In Article”]
As Microsoft provides in its documents, the below words are also reserved for future use. Currently nothing is associated with it.
For future use
If you try to use any of the words about, PowerShell will not treat it as string of characters. PowerShell will try to apply any action based on the special meaning of this word that has been predefined.
If you really need to use those words as part of your script or as parameter arguments, you need to enclose these words in quotation marks.
$a = exit $b = if $c = switch
If you try to run any of the above you will not be able. As soon as you write the first command, PowerShell will exit. The second and third commands will give you an error as PowerShell is expecting you to finish if and switch statements. In order to use those words and save them as strings you have to enclose them in quotation marks. By typing the below you are able to save those variables without any issues.
$a = "exit" $b = "if" $c = "switch"
I hope the tutorial about PowerShell Reserved Words is helpful.
Please let me know your comments and thoughts.
You feedback is appreciated.
[adinserter name=”In Article”]
- PowerShell Tutorials
- PowerShell Scripts
- about_Reserved_Words | Microsoft Docs
- About Language Keywords | Microsoft Docs
Leave a Reply