liveBook · Manning

Binding

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A variable is a named storage location that holds a value or collection of values and can be assigned a new value.

A variable is a named place to store a value or collection of objects for later use. In PowerShell, use the dollar sign to access the contents; it is not part of the variable name. Variables can hold computer names, services, processes, XML documents, or other objects. section 16.0 section 16.1

Store and reuse a value

Assign a value with the equals sign, then use the variable wherever the value belongs: $var = "SRV-02" $var A variable can replace a literal command argument: $var = "13481" Get-Process -Id $var PowerShell does not require a separate declaration before first use, and variables normally disappear when the shell session closes. section 16.2 section 16.1

Use these naming and assignment habits:

  • Choose meaningful, succinct names such as $computername; avoid spaces even though ${My Variable} is supported. section 16.2 section 16.8
  • Assign a comma-separated list to store multiple objects: $computers = 'SRV-02','SERVER1','localhost'. section 16.4
  • Read collection elements with indexes beginning at 0; use -1 for the last element and Count to see the number of objects. section 16.4.1
  • Assign a new value to replace the entire variable or an indexed element; methods such as ToLower() return new strings rather than changing the original. section 16.4.1 section 16.4.2
PS > $computername = 'SRV-02'
PS > $phrase = "The computer name is $computername"
PS > $phrase
The computer name is SRV-02

Use double quotation marks when a string should substitute a variable's current contents during parsing. section 16.3

Single-quoted strings treat variable text literally. Double-quoted strings substitute embedded variables when the string is first parsed, so changing the original variable later does not update the already-created string. Escape a dollar sign with the backtick when you want the variable name displayed, and use `n for a newline. section 16.3

Control the stored type

Input from Read-Host arrives as a string. Multiplying the string "100" by 10 repeats its text instead of performing numeric multiplication. Declare an integer when assigning the input: [int]$number = Read-Host "Enter a number" $number = $number * 10 Entering 100 produces 1000. Entering nonnumeric text raises a conversion error. Type declarations can therefore prevent later logic errors when a variable is expected to contain one kind of object. section 16.6 section 16.8

Process collections with indexing, pipelines, or ForEach-Object. For example, pipe $computers to ForEach-Object and assign the resulting lowercase strings back to $computers; use Select-Object to select a property such as Length. Variables also make commands easier to parameterize: store a computer name or path, then substitute it into a repeatable script. section 16.4.3 section 19.3

PowerShell provides New-Variable, Set-Variable, Remove-Variable, Get-Variable, and Clear-Variable, but ordinary assignment usually handles creation, reading, and updating more simply. Scope controls where variables exist: the global scope contains script scopes, while functions have private scopes; a scope disappears when its execution ends. section 16.7 section 19.7

Administrator workflow

Store command results for inspection or export. A background job can retrieve processes from two computers, receive its results into a variable, display them, and export them to CLIXML. A service query can be stored, narrowed to bits and the print spooler service, displayed, and exported to CSV. section 16.10

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains assignment, naming, quoting, collections, types, pipelines, cmdlets, and scope through runnable administrative examples.
Examples
Uses computer names, process identifiers, collections, quoted phrases, numeric input, background jobs, services, CSV, and CLIXML.
What it assumes
Assumes basic PowerShell commands and pipelines while teaching variable syntax, object storage, collections, and types from the beginning.
What you can do after
Apply variables to store, inspect, transform, type, export, and parameterize PowerShell data and commands.
How it connects
Connects variables with assignment operators, string interpolation, arrays, object properties, pipelines, type conversion, parameterization, and scope.

Also explained in

Compare how 11 books explain binding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.