liveBook · Manning

Chapter 26: Tips, tricks, and techniques

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

This section explains how PowerShell profile scripts customize shell startup and how their location, host, and execution policy affect when they run.

After this chapter you will be able to

What it builds on

Section by section

Introduction

26.1 Profiles, prompts, and colors: Customizing the shell

26.1.1 PowerShell profiles

This section explains how PowerShell profile scripts customize shell startup and how their location, host, and execution policy affect when they run.

Key terms: hosting application, PowerShell engine, profile scripts, $pshome, $home, console host, VS Code, profile.ps1, $Profile, Format-List -force

26.1.2 Customizing the prompt

This subsection explains how the Prompt function generates PowerShell’s prompt and how a profile script can customize it.

Key terms: Prompt, profile script, $DebugContext, VARIABLE: drive, Get-Location, $nestedpromptlevel, nested prompt, double quotation marks, $time, current computer name

26.1.3 Tweaking colors

This subsection shows how to change PowerShell’s default window colors and message colors.

Key terms: foreground, background, Properties, Colors tab, profile, ErrorForegroundColor, WarningForegroundColor

26.2 Operators: -as, -is, -replace, -join, -split, -contains, -in

26.2.1 -as and -is

This subsection introduces -as for converting an object to another type and -is for checking an object's type.

Key terms: -as, type, casting, [int], -is, True, False

26.2.2 -replace

Explains how PowerShell's -replace operator uses a regular expression to replace matching text and how it differs from the string replace() method.

Key terms: -replace operator, regex, source string, string replace() method

26.2.3 -join and -split

This section shows how to turn an array into a delimited string with -join and turn a delimited string back into an array with -split.

Key terms: -join, array, pipe-delimited string, -split, tab-delimited, backtick, `t, index numbers

26.2.4 -contains and -in

Distinguishes wildcard string matching from collection membership tests and explains the operand order for -contains and -in.

Key terms: -like, -contains, -in, wildcard string comparisons, collection, operands

26.3 String manipulation

This section shows how to discover and use methods on PowerShell strings to find text, change case, and remove whitespace.

Key terms: strings, objects, methods, gm, IndexOf(), ToLower(), ToUpper(), Trim(), TrimStart(), TrimEnd()

26.4 Date manipulation

This section shows how DateTime properties and methods provide date parts, calculate dates, and format dates and times.

Key terms: DateTime, properties, methods, AddDays(), To

26.5 Dealing with WMI dates

Explains how to make WMI date strings usable in Windows PowerShell 5.1 and contrasts them with date values returned by CIM commands.

Key terms: WMI, ConvertFromDateTime(), ConvertToDateTime(), Select-Object, Format-Table, calculated columns and properties, CIM commands, date/time values

26.6 Setting default parameter values

This section shows how to set default PowerShell parameter values and explains when those defaults apply.

Key terms: $PSDefaultParameterValues, hash table, profile script, Add(), wildcards, script block, -Credential, Get-Credential, scope, shell session

26.7 Playing with script blocks

The section identifies where script blocks appear in familiar PowerShell commands and shows how to recognize, store, and run one.

Key terms: script block, curly brackets, hash table, call operator, Where-Object, ForEach-Object, Expression, -ScriptBlock

26.8 More tips, tricks, and techniques

The section encourages readers to keep improving their PowerShell skills by picking up useful tips over time.

Key terms: tips and techniques, PowerShell.org, incrementally

Concepts explained in this chapter

← Chapter 25: Debugging techniques Chapter 27: Never the end →

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.