Skip to content

Strings

  • Quotes:
    • Single quotes '...': Literal, does not parse variables or escape sequences
    • Double quotes "...": Interpolated string, parses variables and ` escape sequences
  • Concatenation: Use + or interpolation
  • Multi-line strings (Here-String): @"..."@ or @'...'@

Example:

powershell
$name = 'Alice'
"Hello, $name"
'Path: C:\\Temp'  # Single quotes don't need to escape backslashes
@"
Line1
Line2 $name  # Supports interpolation
"@

Formatting:

powershell
'{0} scored {1:P2}' -f 'Alice', 0.8765

Regular expressions: -match and $Matches; -replace supports regex replacement.

powershell
'abc123' -match '^(?<prefix>[a-z]+)(?<num>\d+)$' | Out-Null
$Matches.prefix  # abc
$Matches.num     # 123
'foo bar' -replace '\s', '_'  # foo_bar

Content is for learning and research only.