Use of Variables

When working with Windows PowerShell, the default is that you don't need to declare variables prior to use; the variable is declared when you use it to hold data. All variable names must be preceded with a dollar sign. There are a number of special variables in Windows PowerShell. These variables are created automatically and each has a special meaning. Table 2-2 lists the special variables and their associated meanings.

Table 2-2. Use of Special Variables

Name

Use

$^

Contains the first token of the last line input into the shell.

$$

Contains the last token of the last line input into the shell.

$_

The current pipeline object; used in script blocks, filters, Where-Object, ForEach-Object, and switch.

$?

Contains the success/fail status of ...

Get Windows PowerShell™ Scripting Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.