Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

28.1. Punctuation

There’s no doubt that PowerShell is full of punctuation, and much of it has a different meaning in the help files than it does in the shell itself. Here’s what it all means within the shell:

  • ` (backtick)—This is PowerShell’s escape character. It removes the special meaning of any character that follows it. For example, a space is normally a separator, which is why cd c:\Program Files generates an error. Escaping the space, cd c:\Program` Files, removes that special meaning and forces the space to be treated as a literal, so the command works.

  • ~ (tilde)—When used as part of a path, this represents the current user’s home directory, as defined in the UserProfile environment variable.

  • ( ) (parentheses)—These are used in a couple of ways:

    • Just as in math, parentheses define an order of execution. PowerShell will execute parenthetical commands first, from the innermost parentheses to the outermost. This is a good way to run a command and have its output feed the parameter of another command: Get-Service -computerName (Get-Content c:\computernames.txt)

    • Parentheses also enclose the parameters of a method, and they must be included even if the method doesn’t require any parameters: ChangeStartMode('Automatic'), for example, or Delete().

  • [ ] (square brackets)—These have two main uses in the shell:

    • They contain the index number when you want to refer to a single object within an array or collection: $services[2] gets the third object from $services (indexes are always zero-based).

    • They contain a data type when you’re casting a piece of data as a specific type. For example, $myresult / 3 -as [int] casts the result as a whole number (integer), and [xml]$data = Get-Content data.xml will read the contents of Data.xml and attempt to parse it as a valid XML document.

  • { } (curly braces or curly brackets)—These have three uses:

    • They contain blocks of executable code or commands, called script blocks. These are often fed to parameters that expect a script block or a filter block: Get-Service | Where-Object { $_.Status -eq 'Running' }

    • They contain the key-value pairs that make up a new hashtable. The opening brace is always preceded by an @ sign. Notice that in this example I’m using braces both to enclose the hashtable key-value pairs (of which there are two) and to enclose an expression script block, which is the value for the second key, “e”:

      $hashtable = @{l='Label';e={expression}}
    • When a variable name contains spaces, braces must surround the name: ${My Variable}

  • ' ' (single quotation marks)—These contain string values. PowerShell doesn’t look for the escape character, nor does it look for variables, inside single quotes.

  • " " (double quotation marks)—These contain string values. PowerShell looks for escape characters and the $ character inside double quotes. Escape characters are processed, and the characters following a $ symbol (up to the next white space) are taken as a variable name and the contents of that variable are substituted. For example, if the variable $one contains the value World, then $two = "Hello $one `n" will contain Hello World and a carriage return (`n is a carriage return).

  • $ (dollar sign)—This character tells the shell that the following characters, up to the next white space, represent a variable name. This can be tricky when working with cmdlets that manage variables. Supposing that $one contains the value two, then New-Variable -name $one -value 'Hello' will create a new variable named two, with the value Hello, because the dollar sign tells the shell that you want to use the contents of $one. New-Variable -name one -value 'Hello' would create a new variable $one.

  • % (percent sign)—This is an alias for the ForEach-Object cmdlet.

  • ? (question mark)—This is an alias for the Where-Object cmdlet.

  • > (right angle bracket)—This is a sort of alias for the Out-File cmdlet. It’s not technically a true alias, but it does provide for Cmd.exe-style file redirection: dir > files.txt.

  • + - * / (math operators)—These function as standard arithmetic operators. Note that + is also used for string concatenation.

  • - (dash or hyphen)—This precedes both parameter names and operators, such as -computerName or -eq. It also separates the verb and noun components of a cmdlet name, as in Get-Content, and serves as the subtraction arithmetic operator.

  • @ (at sign)—This has four uses in the shell:

    • It precedes a hashtable’s opening curly brace (see curly braces, above).

    • When used before parentheses, it encloses a comma-separated list of values that form an array: $array = @(1,2,3,4). But both the @ sign and the parentheses are optional, because the shell will normally treat any comma-separated list as an array anyway.

    • It denotes a here-string, which is a block of literal string text. A here-string starts with @" and ends with "@, and the closing mark must be on the beginning of a new line. Run help about_quoting_rules for more information and examples. Here-strings can also be defined using single quotes.

    • It is PowerShell’s splat operator. If you construct a hashtable where the keys match parameter names, and those values’ keys are the parameters’ values, then you can splat the hashtable to a cmdlet. The B# .NET Blog has a “Windows PowerShell 2.0 Feature Focus—Splat, Split and Join” article that provides a good example of splatting (

  • & (ampersand)—This is PowerShell’s invocation operator, instructing the shell to treat something as a command and to run it. For example, $a = "Dir" places the string "Dir" into the variable $a; & $a will run the Dir command.

  • ; (semicolon)—This is used to separate two independent PowerShell commands that are included on a single line: Dir ; Get-Process will run Dir and then Get-Process. The results are sent to a single pipeline, but the results of Dir aren’t piped to Get-Process.

  • # (pound sign or hash mark)—This is used as a comment character. Any characters following #, to the next carriage return, are ignored by the shell. The angle brackets, < and >, are used as part of the tags that define a block comment: Use <# to start a block comment, and #> to end one. Everything within the block comment will be ignored by the shell.

  • = (equal sign)—This is the assignment operator, used to assign a value to a variable: $one = 1. It isn’t used for quality comparisons; use -eq instead. Note that the equal sign can be used in conjunction with a math operator: $var +=5 will add 5 to whatever is currently in $var.

  • | (pipe)—The pipe is used to convey the output of one cmdlet to the input of another. The second cmdlet (the one receiving the output) uses pipeline parameter binding to determine which parameter or parameters will actually receive the piped-in objects. Chapter 7 has a discussion of this process.

  • \ or / (backslash or slash)—A forward slash is used as a division operator in mathematical expressions; either the forward slash or backslash can be used as a path separator in file paths: C:\Windows is the same as C:/Windows. The backslash is also used as an escape character in WMI filter criteria and in regular expressions.

  • . (period)—The period has three main uses:

    • It’s used to indicate that you want to access a member, such as a property or method, or an object: $_.Status will access the Status property of whatever object is in the $_ placeholder.

    • It’s used to dot source a script, meaning that the script will be run within the current scope, and anything defined by that script will remain defined after the script completes, for example, c:\myscript.ps1.

    • Two dots (..) form the range operator, which is discussed later in this chapter. You will also see two dots used to refer to the parent folder in the filesystem, such as in the path ..\.

  • , (comma)—Outside of quotation marks, the comma separates the items in a list or array: "One",2,"Three",4. It can be used to pass multiple static values to a parameter that can accept them: Get-Process -computername Server1, Server2,Server3.

  • : (colon)—The colon (technically, two colons) is used to access static members of a class; this gets into .NET Framework programming concepts. [date-time]::now is an example (although you could achieve that same task by running Get-Date).

  • ! (exclamation point)—This is an alias for the -not Boolean operator.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint