PowerShell Strings
Strings are sequences of characters — letters, numbers, symbols, and spaces enclosed in quotes. Strings appear in almost every script: file paths, messages, user input, log entries, email addresses, and report data. Mastering string operations is essential for real-world PowerShell scripting.
Creating Strings
PowerShell supports two types of string delimiters: double quotes and single quotes. The behavior is different for each.
# Double quotes – variables and escape characters are expanded
$name = "Ravi"
$message = "Hello, $name!"
Write-Host $message # Output: Hello, Ravi!
# Single quotes – everything is treated as literal text
$message = 'Hello, $name!'
Write-Host $message # Output: Hello, $name!
String Escape Characters
Escape characters use the backtick (`) symbol — not the backslash. The backtick tells PowerShell to treat the next character specially.
| Escape Sequence | Meaning |
|---|---|
| `n | New line |
| `t | Tab |
| `r | Carriage return |
| `` | Literal backtick |
| `" | Double quote inside a double-quoted string |
| `0 | Null character |
Write-Host "Line 1`nLine 2"
Write-Host "Name:`tRavi"
Write-Host "He said: `"Hello!`""
Output:
Line 1
Line 2
Name: Ravi
He said: "Hello!"
Here-Strings (Multi-Line Strings)
Here-strings allow multi-line text blocks. They start with @" and end with "@ on its own line.
# Here-string with double quotes (variables expand)
$server = "WebServer01"
$report = @"
Server Report
=============
Server Name : $server
Date : $(Get-Date -Format "yyyy-MM-dd")
Status : Online
"@
Write-Host $report
Output:
Server Report
=============
Server Name : WebServer01
Date : 2026-03-21
Status : Online
# Here-string with single quotes (no variable expansion)
$literal = @'
This is $server
No expansion happens here
'@
Write-Host $literal
Output:
This is $server
No expansion happens here
String Methods
Every string in PowerShell is a .NET System.String object. It comes with built-in methods for manipulation.
Length
$text = "PowerShell"
Write-Host $text.Length # Output: 10
ToUpper and ToLower
$word = "powershell"
Write-Host $word.ToUpper() # POWERSHELL
Write-Host $word.ToLower() # powershell
Trim – Remove Spaces
$padded = " Hello World "
Write-Host $padded.Trim() # "Hello World" – removes both sides
Write-Host $padded.TrimStart() # "Hello World " – removes left side only
Write-Host $padded.TrimEnd() # " Hello World" – removes right side only
Replace
$sentence = "I love Python scripting"
$updated = $sentence.Replace("Python", "PowerShell")
Write-Host $updated # Output: I love PowerShell scripting
Contains
$path = "C:\Windows\System32"
$path.Contains("System32") # Output: True
$path.Contains("Temp") # Output: False
StartsWith and EndsWith
$filename = "report_2026.xlsx"
$filename.StartsWith("report") # True
$filename.EndsWith(".xlsx") # True
$filename.EndsWith(".pdf") # False
IndexOf – Find Position
$text = "Hello World"
$pos = $text.IndexOf("World")
Write-Host $pos # Output: 6 (position starts at 0)
Substring – Extract Part of a String
$text = "PowerShell Scripting"
# Substring(startIndex, length)
Write-Host $text.Substring(0, 10) # PowerShell
Write-Host $text.Substring(11) # Scripting (from index 11 to end)
Split – Break String Into Parts
$csv = "Delhi,Mumbai,Chennai,Kolkata"
$cities = $csv.Split(",")
foreach ($city in $cities) {
Write-Host $city
}
Output:
Delhi
Mumbai
Chennai
Kolkata
Join – Combine Strings with a Separator
$parts = @("2026", "03", "21")
$dateStr = $parts -join "-"
Write-Host $dateStr # Output: 2026-03-21
$words = @("PowerShell", "is", "powerful")
$sentence = $words -join " "
Write-Host $sentence # Output: PowerShell is powerful
String Formatting
Using -f Format Operator
The -f operator inserts values into a formatted string template using numbered placeholders {0}, {1}, etc.
$name = "Priya"
$score = 95
$result = "Student: {0}, Score: {1}" -f $name, $score
Write-Host $result
Output:
Student: Priya, Score: 95
Number Formatting
$amount = 150000.75
# Format with 2 decimal places
"{0:F2}" -f $amount # 150000.75
# Format with comma separators
"{0:N2}" -f $amount # 150,000.75
# Format as currency (uses system locale)
"{0:C}" -f $amount # ₹1,50,000.75 (on Indian locale)
# Format as percentage
$rate = 0.185
"{0:P1}" -f $rate # 18.5%
String Comparison
$s1 = "PowerShell"
$s2 = "powershell"
# Case-insensitive (default)
$s1 -eq $s2 # True
# Case-sensitive
$s1 -ceq $s2 # False
# Alphabetical comparison
$s1 -lt "Zsh" # True (P comes before Z)
Concatenation vs Interpolation
$brand = "Microsoft"
$tool = "PowerShell"
# Concatenation (using + operator)
$result = $brand + " " + $tool
Write-Host $result # Microsoft PowerShell
# Interpolation (embedding in string – cleaner)
$result = "$brand $tool"
Write-Host $result # Microsoft PowerShell
# Subexpression inside a string – for expressions
$result = "Today is $(Get-Date -Format 'dd-MM-yyyy')"
Write-Host $result # Today is 21-03-2026
Check If a String Is Empty or Null
$input1 = ""
$input2 = $null
$input3 = " "
# IsNullOrEmpty – checks for null or empty string
[string]::IsNullOrEmpty($input1) # True
[string]::IsNullOrEmpty($input2) # True
[string]::IsNullOrEmpty($input3) # False (spaces are not empty)
# IsNullOrWhiteSpace – checks for null, empty, or only spaces
[string]::IsNullOrWhiteSpace($input3) # True
Common String Tasks – Quick Reference
| Task | Method or Operator | Example |
|---|---|---|
| Get length | .Length | "hello".Length → 5 |
| Uppercase | .ToUpper() | "hi".ToUpper() → HI |
| Lowercase | .ToLower() | "HI".ToLower() → hi |
| Remove spaces | .Trim() | " hi ".Trim() → hi |
| Replace text | .Replace() | "a,b".Replace(",","-") → a-b |
| Split by char | .Split() | "a,b".Split(",") → [a,b] |
| Join array | -join | @("a","b") -join "," → a,b |
| Extract part | .Substring() | "Hello".Substring(0,3) → Hel |
| Search | .Contains() | "Hello".Contains("ell") → True |
| Format | -f | "{0} items" -f 5 → 5 items |
Summary
Strings in PowerShell are powerful, feature-rich objects. Double quotes expand variables while single quotes preserve literal text. Here-strings handle multi-line content elegantly. Methods like Split, Replace, Trim, and Substring handle all common text manipulation tasks. The -f operator formats output cleanly for reports and messages. These tools handle virtually every string processing task that appears in real scripts.
