PowerShell Multiline Strings (Here-Strings) With Examples

Create a multiline string with a PowerShell here-string

Got a block of text that won’t fit on one line? A PowerShell multiline string is easiest as a here-string: put @” at the end of a line, your text below it, and “@ at the start of the last line: Use @’ ‘@ instead when variables inside should stay as plain text. In this … Read more

PowerShell Multiline Command: Split a Long Command Across Lines

Split a long PowerShell command across multiple lines

To write a PowerShell multiline command, end each line with something that can’t finish a statement, like a pipe |, a comma or an opening bracket. PowerShell then reads the next line as part of the same command: For a long list of parameters, put them in a hashtable and splat it. The backtick works … Read more

Mandatory Parameters in PowerShell (With Examples)

Mandatory parameters in PowerShell with the Parameter attribute

To make a parameter mandatory in PowerShell, put [Parameter(Mandatory)] right above it in the param block. If the caller leaves it out, PowerShell stops and asks for the value: In this tutorial, I’ll show you how mandatory parameters in PowerShell behave: the prompt, help messages, arrays, empty strings, parameter sets and scripts that run unattended. … Read more

PowerShell String Interpolation (Variables in Strings)

PowerShell string interpolation with variables and subexpressions

PowerShell string interpolation means writing a variable straight into a double-quoted string, and PowerShell swaps in its value when the line runs. Wrap anything more than a plain variable in $(): In this tutorial, I’ll build an order message for a small online store, one step at a time, and hit every trap on the … Read more

How to Clear the Screen in PowerShell (cls, Clear-Host)

Clear the screen in PowerShell with cls and Clear-Host

An hour of testing leaves the console buried in red errors, old tables and half-finished commands. You want a clean slate without closing the window. Quick answer: to clear the screen in PowerShell, type cls (or clear, or Clear-Host) and press Enter. You can also press Ctrl+L at the prompt: In this tutorial, we’ll show … Read more

Fix: You Cannot Call a Method on a Null-Valued Expression

The error You cannot call a method on a null-valued expression shown in a PowerShell 7 console

“You cannot call a method on a null-valued expression” means the thing before .Method() is $null, usually because a variable was never set or a command returned nothing. Check it first: In this tutorial, I’ll reproduce you cannot call a method on a null-valued expression in PowerShell 7 and 5.1, then walk through the causes … Read more

ConvertTo-SecureString in PowerShell (With Examples)

ConvertTo-SecureString and ConvertFrom-SecureString round trip through a DPAPI-encrypted file in PowerShell 7

ConvertTo-SecureString turns a string into a System.Security.SecureString, the type that -Password and -Credential parameters expect. For plain text, add -AsPlainText and -Force: In this tutorial, I’ll walk through ConvertTo-SecureString the way scripts actually use it: plain text, encrypted strings saved to a file, AES keys for other computers, and credential objects. Every password here is … Read more

Get-Item in PowerShell: Get Files, Folders and Registry Keys

Get-Item in PowerShell 7 showing the name, size, date and attributes of a CSV file

Get-Item hands you one object for the exact path you give it, whether that’s a file, a folder or a registry key. In PowerShell it doesn’t list what’s inside a folder; it returns the folder itself: In this tutorial, I’ll walk you through Get-Item in PowerShell on a small sample folder: file properties, wildcards, -LiteralPath, … Read more

How to Shut Down a Computer Using PowerShell (Stop-Computer)

Stop-Computer with -WhatIf in PowerShell 7 previewing a shutdown of three remote computers

To shut down a computer with PowerShell, run Stop-Computer. With no parameters it turns off the local computer right away, and -ComputerName points it at other machines: In this tutorial, I’ll show you how to shutdown computer PowerShell style: the local PC, a list of remote servers, a delayed or scheduled shutdown, and the parameters … Read more

Start-Transcript in PowerShell: Log Everything in a Session

Start-Transcript in PowerShell recording output, Write-Host, verbose and warning lines

When a 2 AM account cleanup goes wrong, the first question is always “what did the script print?” Start-Transcript answers it: it writes everything the session shows on screen to a text file until you run Stop-Transcript. In this tutorial, I’ll show you how Start-Transcript works in PowerShell 7 and Windows PowerShell 5.1: where the … Read more