Variables

Windows PowerShell provides you several ways to define and access variables, as summarized in the following table:

Windows PowerShell variable syntaxes

Syntax

Meaning

$simpleVariable = "Value"

A simple variable name. The variable name must consist of alphanumeric characters. Variable names are not case sensitive.

${arbitrary!@#@#`{var`}iable} = "Value"

An arbitrary variable name. The variable name must be surrounded by curly braces, but may contain any characters. Curly braces in the variable name must be escaped with a backtick (`).

${c:\filename.extension}

Variable “Get and Set Content” syntax. This is similar to the arbitrary variable name syntax. If the name corresponds to a valid PowerShell path, you can get and set the content of the item at that location by reading and writing to the variable.

[datatype] $variable = "Value"

Strongly typed variable. Ensures that the variable may contain only data of the type you declare. PowerShell throws an error if it cannot coerce the data into this type when you assign it.

$SCOPE:variable

Gets or sets the variable at that specific scope. Valid scope names are global (to set a variable available to the entire shell,) script (to set a variable available only to the current script,) local (to set a variable available only to the current scope and subscopes,) and private (to set a variable available only to the current scope.)

Note

Unlike some languages, PowerShell rounds numbers (rather than truncate them) when it converts them to the [int] datatype: ...

Get Windows PowerShell Quick Reference 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.