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

Doctor Scripto

Dr Scripto

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.

 

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

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.

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

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

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

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

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.

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

 

Doctor Scripto
Dr Scripto

Scripter, PowerShell, vbScript, BAT, CMD

Follow Dr Scripto   

3 comments

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

  • Avatar
    Jim Vierra

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

  • Avatar
    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 needed.
    $letter = $template -f $username, $userid, $password, $office

Leave a comment