Chapter 2. PowerShell scripting overview

 

When you start using PowerShell’s scripting language, it’s easy to run into a number of gotchas and hurdles that you wouldn’t ordinarily see when you’re running commands. In this chapter, we’ll try to get those hurdles out of the way up front, so that you can start creating tools with fewer hassles.

2.1. What is PowerShell scripting?

From our perspective, PowerShell is first and foremost a command-line interface (CLI). That means you run commands and get immediate results. Like many good CLIs, PowerShell contains a scripting language, but using that language is optional. To make tools, we’ll definitely be using that language! The good news is that compared to languages like VBScript, Perl, PHP, and others, PowerShell’s language is incredibly simplified, consisting of only about three dozen keywords.

At its simplest, scripting might just involve running several specific commands in a specific sequence. More complex scripts might start applying logic—only execute this command if that condition exists. Scripts might have to execute some task across a number of different targets—checking a number of different files, reconfiguring a number of different services, and so on. The point is automation, in most cases, typically completing some series of steps that you could do manually but that you’d rather codify into a tool that can be reused and repeated more easily, perhaps by folks other than yourself.

2.2. PowerShell’s execution policy

2.3. Running scripts

2.4. Editing scripts

2.5. Further exploration: script editors

2.6. Lab

sitemap