liveBook · Manning

Assignment

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

An assignment operator is a language operator that assigns a value to a variable.

Assignment stores a value in a variable. In PowerShell, write the variable name, an equals sign, and the value you want to keep. The dollar sign signals that what follows is a variable name; it is not part of the name itself. section 16.2

Store and reuse a computer name

Try this with your test server’s name: $var = "SRV-02". Entering $var afterward displays SRV-02. The same pattern lets you reuse values in commands, such as assigning a process ID and running Get-Process -Id $var. section 16.2

Keep these practical details in mind:

  • Use sensible variable names containing letters, numbers, and underscores; names can be long, while names with spaces require curly braces. section 16.2
  • Variables disappear when the shell session closes. section 16.2
  • Prefer normal assignment syntax over variable-management cmdlets when writing scripts, because PowerShell can better predict the value’s data style. section 16.7
$var = "SRV-02"

See the minimal assignment command: $var = "SRV-02". section 16.2

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

How deep it goes
Defines assignment, demonstrates reuse, and explains naming and lifetime, while deferring out-of-scope variables and broader scope.
Examples
Demonstrates assigning a server name or process ID, retrieving the value, and inspecting an assigned integer.
What it assumes
Assumes only a PowerShell shell and test server or process values, while teaching variable syntax from the beginning.
What you can do after
Apply it via PowerShell to store values and substitute them into later commands.
How it connects
Connects assignment with variables, shell sessions, commands, data types, and variable scope.

Also explained in

Compare how 7 books explain assignment →

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.