September 11th, 2019

Maximizing the Power of Here-String in PowerShell for Configuration Data

Doctor Scripto
Scripter

Summary: Discovering some of the awesome and neat ways to leverage a Here-String in a Script or function

Q: Hey, Doctor Scripto!

I used to write scripts with supporting configuration files that never really changed. I was wondering if there was any way to put them INSIDE the script directly to make it self contained.

—AB

A: Hello AB, I was chatting with my good friend Sean on this one. Here he talks about using Here-Strings to solve this problem.

Thanks Doctor Scripto, yes a Here-String can be a great way to embed the configuration or other data for that matter in a script.

If you’ve never encountered a Here-String before, this is an example.

$Message=@'
Hello people, this is Doctor Scripto
Welcome to the Scripting Blog
Today we're showing you a Here-String.
'@

 

When you examine the properties of $Message you’ll see it not an Array but one long string (including carriage returns and linefeed characters within)

Now if the Here-String is enclosed within Single Quotes (just like any other string in PowerShell) it is taken as a literal (Nothing is interpreted). Why you could almost say a Here-String written in this fashion is truly “What You See Is What You Get”.

What is nice about this is if you wanted something in the script to be editable by a NON scripter, such as a welcome message, Placing it in a Literal Here-String makes it much easier for a non tech to do it.

Another example of a Here-String is when you enclose it with Double Quotes (“). When it’s done this way the content is now Evaluated in the parser before it is consumed by the rest the script. Here’s a simple example such as new user Letter

$FirstName='Doctor'
$LastName='Scripto'
$UserID=$Firstname+'.'+$Lastname
$PhoneNumber='425-555-1212'
$Office='Seattle'

$TemporaryPassword=[System.Web.Security.Membership]::GeneratePassword(10,0)

$WelcomeLetter=@"
Hello and welcome to Contoso $FirstName !

Your user id on the system will be $UserID with a Temporary Password of $TemporaryPassword
and your home Office is in $Office. You can be reached at $PhoneNumber.

If you have any issues with your setup you can call 425-555-1111 in order to reach the Help desk.

Thanks!
Your Friendly Neighborhood It Department
"@

If you ran this and went to display the object $WelcomeLetter , instead of seeing Variables it would all expand to real values like this.

Again something that’s a bit easier to edit and view. But how about an array of information? Normally I would read an array in this fashion.

[system.array]$Somelist='Value1','Value2','Value3','Value4'

But for a person who might NOT be a PowerShell Guru, wouldn’t it be cool to place that array in this fashion?

$SomeListToUse=@'
Value1
Value2
Value3
Value4
'@

We can take this string and convert it to an array with a simple use of -replace in PowerShell (to clear out the LineFeeds in the Here-String (ASCII 10).

$SomeList=$SomeListToUse -replace "`n" , ''

Then we can use split method by targeting the “CarriageReturn” (ASCII 13) at the termination of the string to convert it to an Array.

$SomeList=$SomeList.split("`r")

Now an interesting thing to note when converting a Here-String to an array in PowerShell core is the Linefeed character is not used in this environment (think of standard Text file in a Unix environment, you probably won’t see a linefeed anywhere in it)

In which case our conversion would look like this

$SomeList=$SomeListToUse.split("`r")

We could even put in a CSV list in a script in this fashion (Just as if it were an external CSV file) and keep it completely self-contained.

$CSVListTemp=@'
UserID,Domain
DScripto,Contoso
JSmith,Fabrikam
'@

$CSVList=$CSVListTemp -replace "`n" , ''
$CSVList=$CSVList.split("`r")
$CSVData=$CSVList | ConvertFrom-CSV

Of course these are just examples of ways you COULD use a Here-String within a script, not how you MUST.

But I thought it would be interesting to share some thoughts for those of you unfamiliar with Here-Strings. They do provide some interesting options in PowerShell

AB that is all there is to using Here-String for the moment.  But who knows?   They may be more to discover next Week!

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Forum. See you tomorrow. Until then, peace.

Your good friend, Doctor Scripto

PowerShell, Doctor Scripto, Sean Kearney

 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Jim Vierra

    A here string can be used as a template.
    $template = @'Hello and welcome to Contoso {0} ! Your user id on the system will be {1} with a Temporary Password of {2}and your home Office is in {3}. You can be reached at $PhoneNumber. If you have any issues with your setup you can call 425-555-1111 in order to reach the Help desk. Thanks!Your Friendly Neighborhood It Department'@
    We can then use this easily multiple times when...

    Read more
  • Jim Vierra

    Similarly we can split a here string into an array like this.
    $a = @’Value1Value2Value3Value4‘@ -split [char]10$a.Count  # will be 4

  • Jim Vierra

    We only need to do this with Csv text in a here-string.
    @’UserID,DomainDScripto,ContosoJSmith,Fabrikam‘@ | ConvertFrom-Csv
    The function does correct splitting for us.