How to Enable PowerShell Logging (Script Block and Module)

Enable PowerShell logging

To enable PowerShell logging, turn on Script Block Logging, which records the code of every script and command that runs. For Windows PowerShell 5.1, set this registry value as administrator: PowerShell 7 uses its own key and log. Below I check what’s already on, explain the three kinds of logging and show how to enable … Read more

How to Print a Variable in PowerShell (With Examples)

Print a variable in PowerShell

To print a variable in PowerShell, type its name on a line by itself. PowerShell sends the value to the output, which is the same as calling Write-Output. Write-Host prints it too: Output: All three lines look the same on screen, but they don’t behave the same inside functions and scripts. That difference is the … Read more

How to Replace Text in Strings Using PowerShell

Replace text in strings using PowerShell

To replace text in a string in PowerShell, use the .Replace() method or the -replace operator. Both return a new string with the changes: Output: They look interchangeable, but they aren’t. .Replace() matches exact text and cares about case. -replace uses regular expressions and ignores case. I ran every example in PowerShell 7.6 and Windows … Read more

How to Find a Character in a String Using PowerShell

Find a character in a string with PowerShell

To find a character in a string with PowerShell, use Contains() to check whether it’s there, or IndexOf() to get its position. IndexOf counts from 0 and returns -1 when the character isn’t found: Output: Below I cover the operator versions, case sensitivity, the second or nth occurrence, every position and counting matches. Every example … Read more

PowerShell Output to File and Console at the Same Time

PowerShell output to file and console

To send PowerShell output to a file and the console at the same time, pipe it to Tee-Object -FilePath. It writes the output to the file and passes the same output on to the screen: Output: The file gets the text exactly as it looks on screen, table layout included. I ran every example here … Read more

PowerShell Functions: Return Values and Multiple Values

PowerShell functions return values

A PowerShell function returns every value it outputs, not just what follows the return keyword. Any expression you don’t assign or discard goes back to the caller: Output: return only ends the function early. Below I show the most common surprise with extra output, and the clean ways to return one value, several values or … Read more

PowerShell Out-File: Syntax, Parameters and Examples

PowerShell Out-File cmdlet with examples

PowerShell Out-File sends command output to a text file, formatted exactly as it would appear in the console. Pipe any command to it and give it a path: This guide covers every parameter you’ll actually use, with tests in PowerShell 7 and 5.1. If you’re still choosing between Out-File, Set-Content and Export-Csv, start with saving … Read more

PowerShell Global Variables and Scopes Explained

PowerShell global variables and scopes

A PowerShell global variable is one you create with the $global: prefix. It’s visible everywhere in your session, and it stays there after the script that created it ends: That’s the script set-config.ps1. Here’s what’s left after running it: Output: The normal variable disappeared with the script, and the global one stayed. That’s both the … Read more

How to Open a File in PowerShell (Default App, Notepad, Console)

Open a file in PowerShell

To open a file in PowerShell with its default app, run Invoke-Item with the file’s path. It works like double-clicking the file in File Explorer: Below I show how to preview what will open, open a file in a specific app, and read files right in the console. I tested each command in PowerShell 7.6 … Read more

How to Get a List of Installed Programs Using PowerShell

Get a list of installed programs using PowerShell

To get a list of installed programs using PowerShell, read the Uninstall keys in the registry with Get-ItemProperty. That’s the same data Windows shows in Settings > Apps > Installed apps, and it works without admin rights. Here’s the short version. It checks the 64-bit, 32-bit and per-user locations and prints each program’s name, version … Read more