O'Reilly logo

Windows PowerShell™ 2.0: Administrator’s Pocket Consultant by William R. Stanek

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Common Elements in Scripts

Now that we’ve discussed how to work with scripts, let’s look at common elements you’ll use in scripts, including

  • Comments

  • Initializing statements

  • Conditional statements

  • Control loops

Using Comments and Initializing Statements

Most scripts begin with comments that specify what a script is for and how it is used. PowerShell supports two types of comments:

  • Single-line comments that begin with #. PowerShell interprets everything from the begin-comment mark to the end of the line as a comment. Here is an example:

    $myVar = "$env:computername" #Get the computer name

    Note

    Because values in strings are interpreted differently and # is not interpreted as a special character in a string, you can use # in single-quoted and double-quoted ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required