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 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

How to Write to a Log File in PowerShell

PowerShell write to log file

To write to a log file in PowerShell, use Add-Content with a timestamp from Get-Date. Add-Content creates the file the first time and appends a new line on every call after that: Output: Below I turn this into a reusable Write-Log function, add daily log files and error logging, and show one file-name mistake that … Read more

How to Get a Substring in PowerShell (Start, End, Before and After)

Substring in PowerShell examples

To get a substring in PowerShell, call the Substring method on a string. Substring(start, length) returns length characters from the start position, and Substring(start) returns everything from there to the end: Output: Positions start at 0, so position 3 is the fourth character. I ran every example here in PowerShell 7.6 and Windows PowerShell 5.1, … Read more