on 05-Jan-2009 09:52
Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time. Today's letter is the letter "K". I had some recommendations for "Kill" and "Keep" but I think I found one a little bit more topical. Today I'm going to talk about PowerShell keywords.
First of all, let's talk about keywords. A keyword, in a programming language such as PowerShell, is a word or identifier that has a particular meaning to that language. In most languages, keywords are reserved words - meaning that they cannot be redefined or aliased over. Keywords "if" and "else" may be familiar as they are common across many languages. While it would be a fun to be able to redefine all the keywords in PowerShell, it might make for potential maintenance issues:
Set-Alias if ifnot
ifnot ($val) { ...}
Probably not such a good idea...
The Keyword List
So, the search for the list of keywords in the PowerShell language turned out to be a tricky one. The best source of the Grammar rules for the PowerShell language I've found is in Appendix C of Windows PowerShell In Action by Bruce Payette which is available as a PDF download here.
Unfortunately there is not a list of language keywords in that document so I had to resort to the next best thing: web search. That drew me to this post at nivot.org where I found a PowerShell one-liner that accesses a undocumented KeywordTokenReader class in the System.Management.Automation namespace to list out the defined keywords. Here's the result for PowerShell V2 CTP3:
PS C:\> [type]::gettype("System.Management.Automation.KeywordTokenReader")|
>> %{$_.InvokeMember("_keywordTokens", "NonPublic,Static,GetField", $null, $_,@())}
Name Value
---- -----
elseif elseif
begin begin
function function
for for
foreach foreach
return return
else else
trap trap
while while
do do
data data
dynamicparam dynamicparam
until until
end end
break break
if if
throw throw
param param
continue continue
finally finally
in in
switch switch
exit exit
filter filter
from from
try try
process process
catch catch
Keyword Definitions
So, now that you have the list of keywords, here's a brief summary of each one and the context in which they are used:
elseif - An additional condition of an if clause.
begin - Described here, the begin keyword is used to specify the clause to run before the first pipeline object is available to a function.
function - Specifies the creation of a function script block.
for - Used to indicate a basic counting loop.
foreach - Allows for easy operation on collections.
return - Halts processing of the rest of the code in the current scope (function or script) and returns control to the caller.
else - Indicates a code block to execute if all of the if/elseif comparisons fail in an if statement.
trap - Allows one to catch and process runtime exceptions.
while - The basic construct for creating a loop.
do - A bottom tested variant of a while loop.
data - Defines a code block that only contains data and is especially useful for internationalization.
dynamicparam - Allows for conditionally defined parameters within function scripts.
until - Used in Do-Until loop that runs only while the condition is false.
end - Specifies a clause that is executed once all the objects have been processed in a function.
break - Immediately terminates a containing loop.
if - Run code blocks if a specified condition test evaluates to true.
throw - Allows for the creation of terminating errors or exceptions.
param - Declare formal parameters for a script or function.
continue - Causes the flow of execution to jump back to the beginning of the containing loop.
finally - Error handling in scripts can be handled with try, catch, and finally script blocks.
in - Used with the foreach keyword to indicate the collection to iterate upon.
switch - A statement that combines pattern matching, branching, and iteration into a single control structure.
exit - Exit the currently being processed script.
filter - Unlike a function that is processed once, a filter is a script block that is run once for each input object coming from the pipeline.
from - Not sure about this one as it's not currently supported in PowerShell V2 CTP3.
try - Error handling in scripts can be handled with try, catch, and finally script blocks.
process - specifies a clause in a function that is processed once for each object in the pipeline.
catch - Error handling in scripts can be handled with try, catch, and finally script blocks.
So, now the next time you are at a geek-dinner with all your PowerShell buddies, you can brag that you know all 28 PowerShell keywords!
-Joe