Skip to content

PowerShell String Manipulation

How to create, concatenate, format, and parse strings in PowerShell using operators, methods, and regex for text processing tasks.

In PowerShell, text is represented by the System.String .NET class. Because strings are full .NET objects rather than simple streams of bytes, PowerShell provides direct access to both native object methods and shell-level operators for string interpolation, regular expression matching, dynamic formatting, and multi-line parsing.

The most common operations on strings in PowerShell are explained below.

Create a String

A string can be created by assigning text to a variable. PowerShell supports both single-quoted (') and double-quoted (") string literals.

Example of String creationpowershell
$message = "Hello World"
Write-Output $message
Outputplaintext
Hello World

Empty String

An empty string can be created by assigning an empty value to a variable. To check whether a string is empty, use [string]::IsNullOrEmpty() or compare it against "".

Empty string initializationpowershell
$variable_name = ""
Empty String Checkpowershell
if ([string]::IsNullOrEmpty($variable_name)) {
    Write-Output "The string is empty"
}

Below is a complete example with empty strings:

Example of Empty Stringpowershell
$a = ""
$b = "Hello"

if ([string]::IsNullOrEmpty($a)) { Write-Output "String A is empty" }
if ([string]::IsNullOrEmpty($b)) { Write-Output "String B is empty" }
Outputplaintext
String A is empty

String Concatenation

There are two primary ways to combine strings: the concatenation operator (+) and string interpolation (using double quotes).

Concatenation with the + Operator

You can combine strings using the arithmetic addition operator (+):

Example of String Concatenationpowershell
$first  = "Power"
$second = "Shell"
$combined = $first + $second
Write-Output $combined
Outputplaintext
PowerShell

You can also concatenate multiple variables and literals directly:

Example of String Concatenation with Env Varspowershell
$Greeting = "Hello, " + $env:USERNAME + "! Welcome to " + $env:COMPUTERNAME + "."
$Greeting
Outputplaintext
Hello, Tom! Welcome to WORKSTATION-01.

String Interpolation

Interpolation embeds variable values directly inside a double-quoted string, avoiding manual concatenation.

Example of String Interpolationpowershell
$name = "Tom"
$age  = 30
Write-Output "Hello, $name! You are $age years old."
Outputplaintext
Hello, Tom! You are 30 years old.

where:

  • Variables prefixed with $ inside a double-quoted string are automatically replaced with their values.
  • For expressions or property access, wrap them in $(...): "Total: $($price * 2)".
Example of Interpolation with Expressionspowershell
$a = "Hello"
$b = "World"
$d = 123
$c = "$a and $b $d"
Write-Output $c
Outputplaintext
Hello and World 123
Example of Single vs Double Quotespowershell
$TargetHost = "sql-prod-01"

Write-Output "Double quotes: Connecting to $TargetHost"
Write-Output 'Single quotes: Connecting to $TargetHost'
Outputplaintext
Double quotes: Connecting to sql-prod-01
Single quotes: Connecting to $TargetHost

Subexpression Evaluation $()

When referencing an object property or method inside a double-quoted string, a bare variable reference evaluates only the base object name, leaving the property name as literal text. To force PowerShell to evaluate an entire property path or expression, wrap it in the subexpression operator $():

Example of Subexpression Evaluationpowershell
$Service = Get-Service -Name "wuauserv"

Write-Output "Incorrect: $Service.DisplayName is currently $Service.Status"

Write-Output "Correct: $($Service.DisplayName) is currently $($Service.Status)"

where:

  • $Service.DisplayName outside of $() causes PowerShell to call $Service.ToString() and append the literal string .DisplayName.
  • $($Service.DisplayName) forces the parser to resolve the actual property value before embedding it into the string.
Outputplaintext
Incorrect: System.ServiceProcess.ServiceController.DisplayName is currently System.ServiceProcess.ServiceController.Status
Correct: Windows Update is currently Running

Here-Strings

A here-string is a string literal used to declare multi-line text blocks without needing escape characters for embedded quotation marks. Here-strings start with @" and end with "@ (each on its own line).

Here-String Syntax Rules

  1. The opening token (@" or @') must be followed immediately by a newline. No characters or spaces can appear after the quote on the same line.
  2. The closing token ("@ or '@) must sit on its own line at the beginning of the line (column 0), with no preceding whitespace.
  • @" ... "@ (Double-quoted here-string): Supports variable interpolation and escape sequences.
  • @' ... '@ (Single-quoted here-string): Completely literal; preserves all characters and whitespace as typed.
Example of Here-Stringpowershell
$Server    = "web-node-01"
$IpAddress = "192.168.1.50"

$ConfigFile = @"
[Interface]
NodeName = $Server
BindingIP = $IpAddress
Environment = Production
GeneratedAt = $(Get-Date -Format "yyyy-MM-dd")
"@

$ConfigFile

where:

  • @" ... "@ encloses the multi-line block.
  • $Server, $IpAddress, and the $(Get-Date ...) subexpression are evaluated dynamically.
Outputplaintext
[Interface]
NodeName = web-node-01
BindingIP = 192.168.1.50
Environment = Production
GeneratedAt = 2026-09-27

String Length

Unlike Batch Scripting, PowerShell strings expose a built-in .Length property that returns the number of characters.

Example of String Lengthpowershell
$str = "Hello World"
Write-Output "String is $($str.Length) characters long"
Outputplaintext
String is 11 characters long

String to Integer

A string containing numeric digits can be converted to an integer using a cast ([int]) or the -as operator. This allows arithmetic operations to be performed.

Example of String to Integerpowershell
$var = "12345"
$var = [int]$var + 5
Write-Output $var
Outputplaintext
12350

You can also use -as for safe conversion that returns $null on failure:

Example of Safe Conversion with -aspowershell
$value = "42"
$number = $value -as [int]
Write-Output ($number + 8)
Outputplaintext
50

Common String Methods

Because PowerShell strings are .NET System.String instances, they inherit built-in transformation and inspection methods.

Method Description Example Result
.Substring(start, length) Extracts a segment starting at an index "Production".Substring(0, 4) "Prod"
.Replace(old, new) Replaces literal character sequences "Server01".Replace("01", "99") "Server99"
.Split(char) Splits a string into an array of substrings "A,B,C".Split(",") @("A", "B", "C")
.Trim() Strips leading and trailing whitespace " data ".Trim() "data"
.TrimStart() / .TrimEnd() Strips whitespace from the start or end " data".TrimStart() "data"
.ToUpper() / .ToLower() Converts string to uppercase or lowercase "pwsh".ToUpper() "PWSH"
.Contains(str) Returns boolean if substring exists "DomainAdmin".Contains("Admin") $true
.StartsWith(str) / .EndsWith(str) Checks boundary character sequences "backup.zip".EndsWith(".zip") $true

.Substring(): Left String, Mid String, Right String

Extracts a portion of a string starting at a given index.

Example of Left Stringpowershell
$str = "Helloworld"
Write-Output $str
$str = $str.Substring(0, 5)
Write-Output $str
Outputplaintext
Helloworld
Hello
Example of Mid Stringpowershell
$str = "Helloworld"
Write-Output $str
$str = $str.Substring(3, 7)
Write-Output $str
Outputplaintext
Helloworld
loworld
Example of Right Stringpowershell
$str = "This message needs changed."
Write-Output $str
$str = $str.Substring($str.Length - 8)
Write-Output $str
Outputplaintext
This message needs changed.
changed.

.Replace()

Replaces all occurrences of a substring with another string.

Example of Replace a Stringpowershell
$str = "This message needs changed."
Write-Output $str
$str = $str.Replace("needs", "has")
Write-Output $str
Outputplaintext
This message needs changed.
This message has changed.

.Remove(): Remove a Substring

Removes a specified number of characters starting at a given index.

Example of Removepowershell
$str = "Batch scripts is easy. It is really easy."
Write-Output $str
$str = $str.Replace("is ", "")
Write-Output $str
Outputplaintext
Batch scripts is easy. It is really easy.
Batch scripts easy. It really easy.

Remove Both Ends

To remove the first and last character, use .Substring(1, $str.Length - 2).

Example of Remove Both Endspowershell
$str = "Batch scripts is easy. It is really easy"
Write-Output $str
$str = $str.Substring(1, $str.Length - 2)
Write-Output $str
Outputplaintext
Batch scripts is easy. It is really easy
atch scripts is easy. It is really eas

Remove All Spaces

Use .Replace(" ", "") to strip every space from a string.

Example of Remove All Spacespowershell
$str = "This string    has    a  lot  of spaces"
Write-Output $str
$str = $str.Replace(" ", "")
Write-Output $str
Outputplaintext
This string    has    a  lot  of spaces
Thisstringhasalotofspaces

.Split()

Splits a string into an array of substrings based on a delimiter.

Example of Splitpowershell
$result = "apple,banana,cherry".Split(",")
$result
Outputplaintext
apple
banana
cherry

.Trim()

Removes leading and trailing whitespace (or specified characters).

Example of Trimpowershell
$result = "   padded text   ".Trim()
Write-Output "[$result]"
Outputplaintext
[padded text]

Combined Example: Parsing a Log Line

Example of String Methods in Actionpowershell
$RawLog = "  2026-10-24|CRITICAL|Service 'Spooler' terminated unexpectedly.  "

$CleanLog = $RawLog.Trim()
$Segments = $CleanLog.Split("|")

$Timestamp = $Segments[0]
$LogLevel  = $Segments[1]
$Message   = $Segments[2]

[PSCustomObject]@{
    Time    = $Timestamp
    Level   = $LogLevel
    Summary = $Message.Replace("terminated unexpectedly", "crashed")
}

where:

  • .Trim() removes leading and trailing spaces from $RawLog.
  • .Split("|") divides the line into an array of three elements based on the pipe character.
  • .Replace() replaces the target substring with "crashed".
Outputplaintext
Time       Level    Summary
----       -----    -------
2026-10-24 CRITICAL Service 'Spooler' crashed.

Align Right

The -f format operator can align text to the right by adding leading spaces. This is useful for lining up columns of numbers in reports.

Example of Align Rightpowershell
$x = 3000
$y = 2

"X={0,8}" -f $x
"Y={0,8}" -f $y
Outputplaintext
X=    3000
Y=       2

Pattern Matching and Replacement: -match and -replace

While .Replace() handles literal text replacements, PowerShell provides native operators powered by the .NET Regular Expression (regex) engine: -match and -replace.

The -match Operator and $Matches

The -match operator tests whether a string matches a regular expression pattern, returning $true or $false.

When a match is found, PowerShell automatically populates the $Matches automatic variable with a hashtable containing the matched strings and any captured groups.

Example of -matchpowershell
"user@example.com" -match "^\w+@\w+\.\w+$"
Outputplaintext
True

Using capture groups:

Example of -match with Capture Groupspowershell
"Order-2026-0091" -match "Order-(\d+)-(\d+)"
$matches[1]
$matches[2]
Outputplaintext
2026
0091

where $matches[0] holds the full match; $matches[1], $matches[2], etc. hold capture groups.

Using named captures in a real scenario:

Example of -match with Named Groupspowershell
$LogLine = "Error 404: Resource /api/v1/users not found on host web01."

$Pattern = "Error (?<Code>\d{3}): Resource (?<Endpoint>\S+)"

if ($LogLine -match $Pattern) {
    [PSCustomObject]@{
        Status   = "Match Detected"
        HttpCode = $Matches['Code']
        Path     = $Matches['Endpoint']
    }
}

where:

  • \d{3} matches three consecutive numeric digits.
  • (?<Code>...) is a named capture group stored under $Matches['Code'].
  • (?<Endpoint>\S+) captures consecutive non-whitespace characters.
Outputplaintext
Status         HttpCode Path
------         -------- ----
Match Detected 404      /api/v1/users

The -replace Operator

The -replace operator replaces all occurrences matching a regex pattern with a substitution string.

Example of -replacepowershell
"2026-03-14" -replace "-", "/"
Outputplaintext
2026/03/14

Replacing repeated whitespace:

Example of -replace with Whitespacepowershell
"Node01    Running    10.0.0.5" -replace '\s+', ' '
Outputplaintext
Node01 Running 10.0.0.5

Using regex backreferences ($1, $2) to reorganize matched text:

Example of -replace with Backreferencespowershell
"Doe, John" -replace '^(\w+),\s*(\w+)$', '$2 $1'
Outputplaintext
John Doe

String Formatting with -f

The format operator (-f) provides a structured way to assemble strings using .NET composite formatting. Instead of concatenating multiple variables, you supply an indexed template string on the left and a comma-separated list of values on the right.

Syntax:

"<template with {0}, {1}, ...>" -f $val0, $val1

Basic Example

Example of -f Operatorpowershell
$name  = "Jamie"
$score = 92.5

"{0} scored {1:N1}%" -f $name, $score

where:

  • {0} and {1} are placeholders corresponding to the arguments after -f, in order.
  • {1:N1} applies a numeric format specifier (N1), rounding to one decimal place.
Outputplaintext
Jamie scored 92.5%

Common Format Specifiers

Specifier Name Example Input Pattern Formatted Result
{0:N2} Number with decimals 10500.5678 "{0:N2}" -f 10500.5678 10,500.57
{0:D4} Zero-padded integer 42 "{0:D4}" -f 42 0042
{0:P1} Percentage 0.156 "{0:P1}" -f 0.156 15.6%
{0:C} Currency (locale-based) 19.99 "{0:C}" -f 19.99 $19.99
{0:X} Hexadecimal 255 "{0:X}" -f 255 FF
{0,-10} Left-aligned padding "Node" "{0,-10}" -f "Node" "Node "
{0,10} Right-aligned padding "Node" "{0,10}" -f "Node" " Node"

Practical Example: Generating Formatted Storage Summaries

The following script gathers system storage metrics, calculates consumption percentages, and formats the output into clean text strings using the -f operator:

format-storage-report.ps1powershell
$Disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3"

foreach ($disk in $Disks) {
    $driveId     = $disk.DeviceID
    $totalGB     = $disk.Size / 1GB
    $freeGB      = $disk.FreeSpace / 1GB
    $usedPercent = ($disk.Size - $disk.FreeSpace) / $disk.Size

    $reportLine = "Drive {0,-3} | Total: {1,7:N2} GB | Free: {2,7:N2} GB | Used: {3,6:P1}" -f `
        $driveId, $totalGB, $freeGB, $usedPercent

    Write-Output $reportLine
}

where:

  • Drive {0,-3} left-aligns the drive identifier within a 3-character field.
  • {1,7:N2} formats total storage as a 2-decimal number right-aligned in a 7-character field.
  • {3,6:P1} formats the utilization fraction as a single-decimal percentage right-aligned in a 6-character field.

Execute the script from the console:

Consolepowershell
pwsh ./format-storage-report.ps1
Outputplaintext
Drive C:  | Total:  255.40 GB | Free:   42.15 GB | Used:  83.5%
Drive D:  | Total: 1024.00 GB | Free:  512.80 GB | Used:  49.9%
Navigation