PowerShell Here-String

PowerShell Here-String

Here-String is a concept rather than an actual PowerShell cmdlet.  It deals with handling text strings countaining speech marks.

Topics for Here-String in PowerShell


Concept: Here-String @"…"@

The Here-String construction provides an easy way for handling text, it's speciality is dealing with speech marks and other delimiters without the need for inserting escape characters.  Here is a simple example:

$Speech = @"
Joe said: "This is difficult"
Betty said: "No it's not if you use a here-string"

Key point: Place the first @", and especially the final, "@ on their own line.
To be precise, the last thing on the first line must be @"
"@ must be the first thing on the last line.

JSON Here-String Example

$Input = @"
"Team": [
{ "FirstName":"Eddie" , "LastName":"Evans" , "Email":"[email protected]" },
{ "FirstName":"Mira" , "LastName":"Jones" , "Email":"[email protected]" },
{ "FirstName":"Mark" , "LastName":"Williams", "Email":"[email protected]" }

$Victim = $Input | ConvertFrom-JSON
$Victim.Contacts | Format-Table FirstName, LastName, Email -AutoSize

Note 1: Pick-out the crucial @".."@ wrapper

Note 2: Observe how PowerShell deals with the tricky syntax of the JavaScript text.  Check the punctuation of not only the speech marks, but also the colon, commas an two types of bracket.

# ConvertFrom-JSON output result:

FirstName LastName Email 
--------- -------- -----
Eddie Evans [email protected]
Mira Jones [email protected]
Mark Williams [email protected]

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

Differences Between @" and @'.

Be aware of the significance of @" and @'.  PowerShell consistently treats the single quote as meaning literal, don't convert; whereas double quotes cause PowerShell to interpret the command if necessary.  Here is a suitable vehicle to see the difference:

# Result for @"…."@
$Math = @"
One is "$(1*1)"
Two is '$(1+1)'
Three is $(6/2)

One is "1"
Two is '2' 
Three is 3

Note 3: The double quotes @", tells PowerShell to interpret $(1+1) as a variable and display the result as 2.  A single quote @' results in the element being returned as a literal string: '$(1+1)'.

Swap @' and '@ for @" and "@

# Problem: single quote.
One is "$(1*1)"
Two is '$(1+1)'
Three is $(6/2)

Troubleshooting Here-String

Pay attention to sequencing of the at symbol and the quote.  Start with @" (and not "@); finish with "@ (and not @").

Employ the correct type of quotes in the text block.  Employ plain single or double quotes, not the curly alternatives.  Avoid quotation mark with the word left or right: For example ” or alt +0148 (right double quotation mark: ).

See also ConvertFrom-JSON »

Summary of PowerShell's Here-String

The Here-String construction provides an easy way for handling large blocks of text, it's particularly good for data containg lots of speech marks and other delimiters, the benefit of Here-String is you don't need to grapple with escape characters.

If you like this page then please share it with your friends


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-File   • Out-GridView   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.