liveBook · Manning

Chapter 16: Variables: A place to store your stuff

What you will learn in chapter 16 of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Introduces variables as a way to keep PowerShell objects for later use, including outside long scripts.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces variables as a way to keep PowerShell objects for later use, including outside long scripts.

Key terms: variables, objects

16.1 Introduction to variables

This section introduces variables as named, reusable storage in memory and explains PowerShell’s flexible approach to their contents.

Key terms: variable, name, declare, types, objects, array

16.2 Storing values in variables

This section explains how to store simple values in PowerShell variables, retrieve and reuse them, and follow basic naming and lifetime rules.

Key terms: object, System.String, simple value, variable, assignment operator, dollar sign, Get-Member, System.Int32, variable name, curly braces

16.3 Using variables: Fun tricks with quotes

This section explains how quotation marks control variable expansion in PowerShell strings and how the backtick escapes characters.

Key terms: single quotation marks, double quotation marks, literal string, replacement action, parses the string, backtick, escape character, `n

16.4 Storing many objects in a variable

This section shows how a comma-separated list stores several distinct objects in one PowerShell variable.

Key terms: comma-separated list, collections of objects, String types

16.4.1 Working with single objects in a variable

This subsection shows how to access one object in a variable and use its properties and methods.

Key terms: index, square brackets, property, System.String, Length, ToUpper(), ToLower(), Replace()

16.4.2 Working with multiple objects in a variable

This subsection explains how to work with a specific object in a variable containing multiple objects and how to change that object's stored value.

Key terms: variable, object, property, method, assign

16.4.3 Other ways to work with multiple objects

This subsection shows how to apply a method or select a property across multiple objects stored in a variable.

Key terms: ForEach-Object, $_, ToLower(), pipeline, Select-Object, Length property

16.4.4 Unrolling properties and methods in PowerShell

This subsection explains how PowerShell automatically accesses properties and methods on the objects held in a variable containing multiple objects.

Key terms: collection, property, method, implicitly enumerates, unrolls

16.5 More tricks with double quotes

This section shows how double-quoted strings can include the result of a PowerShell expression or a property from every object in a collection.

Key terms: double quotes, variable-replacement, subexpression, $(), collection, property, method, enumerate

16.6 Declaring a variable’s type

This section explains how declaring a variable’s type prevents PowerShell from treating input as the wrong kind of data.

Key terms: string, System.String, System.Int32, Get-Member, [int], object type, convert, retype

16.7 Commands for working with variables

This section explains why ordinary variable syntax is usually preferable to variable cmdlets and shows when and how to use those cmdlets.

Key terms: variable declaration, ad hoc syntax, normal assignment operators, Remove-Variable, Remove-Item, VARIABLE: drive, -name

16.8 Variable best practices

Reviews practical rules for naming variables and declaring their types to make PowerShell code clearer and less error-prone.

Key terms: variable names, type of object, declare, code-hinting

16.9 Common points of confusion

This section clarifies what counts as a PowerShell variable name and how the shell identifies it after a dollar sign.

Key terms: variable name, dollar sign, opening curly brace, closing curly brace, white space

16.10 Lab

16.11 Lab answers

16.12 Further exploration

This section invites readers to spot opportunities to use variables to reuse values, using a remote connection as an example.

Key terms: variables, connection, remote computers

Where this is used later

Concepts explained in this chapter

← Chapter 15: Working with many objects, one at a time Chapter 17: Input and output →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.