Strings
Windows PowerShell offers several facilities for working with plain-text data.
Literal and Expanding Strings
To define a literal string (one in which no variable or escape expansion occurs), enclose it in single quotes:
$myString = 'hello `t $ENV:SystemRoot' $myString gets the actual value of hello `t $ENV:SystemRoot
To define an expanding string (one in which variable and escape expansion occurs), enclose it in double quotes:
$myString = "hello `t $ENV:SystemRoot" $myString gets a value similar to hello C:\WINDOWS
To include a single quote in a single-quoted string, or a double quote in a double-quoted string, you may include two of the quote characters in a row:
PS >"Hello ""There""!" Hello "There"! PS >'Hello ''There''!' Hello 'There'!
Tip
To include a complex expression inside of an expanding string, use a subexpression. For example:
$prompt = "$(get-location) >" $prompt gets a value similar to c:\temp >
Accessing the properties of an object counts as a complex expression:
$output = "Current script name is: $($myInvocation.MyCommand.Path)"
$output
gets a value
similar to Current script name is
c:\Test-Script.ps1
Here Strings
To define a here string (one that may span multiple lines), place the two characters @” at the beginning, and the two characters “@ on their own line at the end.
For example:
$myHereString = @" This text may span multiple lines, and may contain "quotes". "@
Here strings may be of either the literal or expanding variety.
Escape Sequences
Windows PowerShell supports the ...
Get Windows PowerShell Quick Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.