O'Reilly logo

Windows PowerShell Quick Reference by Lee Holmes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required