PowerShell Strings
A string is text in quotes. Single quotes keep text literal; double quotes expand variables inside. Operators like -replace, -split and -join reshape strings, and methods like .Trim() and .ToUpper() clean them up.
Documentation: Microsoft Learn
Overview
Strings show up in almost every script: names, paths, log lines, messages. PowerShell has two quote styles and they behave differently. Single quotes are literal — what you type is what you get. Double quotes expand variables, so "Hello $name" becomes Hello Ada when $name holds Ada.
Three operators do most string work. -replace swaps text using a pattern (it is regex-based, so characters like . and * have special meaning). -split breaks one string into pieces at a delimiter. -join glues pieces back together with a separator you choose.
Methods handle the rest. .Replace() swaps literal text without regex. .Trim() strips surrounding spaces. .ToUpper() and .ToLower() change case. .Substring() cuts out part of a string, and .Contains(), .StartsWith() and .EndsWith() test what a string holds. The -f format operator builds messages: 'Hello {0}' -f $name fills in the placeholder.
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
$city = ' new york '
$city.Trim().ToUpper()- Stage 1$city = ' new york '
Removes the Get- prefix from a command name using -replace.
What happens
The first line stores text with extra spaces on both sides. The second line chains two methods: .Trim() strips the surrounding spaces, then .ToUpper() converts what is left to capitals. The result is NEW YORK — the original variable is unchanged, because string methods return a new string instead of editing in place.
Worth remembering
- Single quotes are literal; double quotes expand variables.
- -replace uses regex; .Replace() swaps literal text.
- -split breaks text apart; -join glues pieces together.
- .Trim() removes surrounding spaces; .ToUpper() and .ToLower() change case.
- String methods return a new string — they never edit the original.
- -f formats messages: 'Hello {0}' -f $name.
Primary documentation
Try it
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Removes the Get- prefix from a command name using -replace.
Show reference output
Illustrative output · values vary by environment
Service