Chapter 19. PowerShell’s scripting language

This chapter covers

  • Logical conditions
  • Loops
  • Branching
  • Code formatting

Although we firmly maintain that PowerShell isn’t a scripting language, it does—like many command-line shells—contain a scripting language. This language can prove useful when you want to automate complex, multipart processes that may require different actions to be taken for different scenarios. PowerShell’s language is definitely simple, consisting of less than two dozen commands usually referred to as keywords, but it’s more than adequate for most jobs. The ability to use cmdlets, functions, and .NET negates the pure language deficiencies. The language’s syntax is loosely modeled on C#, which lends it a strong resemblance to other C-based languages such as PHP, C++, and Java.

19.1. Defining conditions

As with most languages, the heart of PowerShell’s scripting capabilities is based on conditions. You define a condition that keeps a loop repeating or define a condition that causes your script to execute some particular command that it’d otherwise ignore.

The conditions you specify will usually be contained within parentheses and will often use PowerShell’s various comparison operators to achieve either a True or False result. Referred to as Boolean values, these True/False results tell PowerShell’s various scripting language elements whether to take some action, keep executing a command, and so on.

19.2. Loops: For, Do, While, Until

19.3. ForEach

19.4. Break and Continue

19.5. If ... ElseIf ... Else

19.6. Switch

19.7. Mastering the punctuation

19.8. Summary