Prof. Powershell

Here, Here (The Here String)

Order in the script -- with the here string.

Have you ever found yourself trying to do something like this?

PS C:\> $p="Menu 'nSelect a task 'n 1 Get running services 'n 2 Get processes '
n 3 Get recent system errors"
PS C:\> Read-host $p
Menu
Select a task
 1 Get running services
 2 Get processes
 3 Get recent system errors: _

It kinda, sorta works but I think it's a real pain to construct, especially trying to get the new lines just right. A better approach is to use a here string. This is a great technique for creating a multiline string. Here is an example:

PS C:\> $h=@"
>> I am
>> the second line
>> the third line
>> the last line
>> "@
>>

The variable $h is a string:

PS C:\> $h.gettype().name
String

But it retains the line returns:

PS C:\> $h
I am
the second line
the third line
the last line

One thing to be aware of when creating here strings in a script: The command must not be indented at all. If you use the ISE you'll see a properly defined here string in dark red. But now it is much easier to do something like this:

#define a prompt
$p=@"
\\\\\\\\
  Menu
////////

  1 - Get Running Services
  2 - Get Processes
  3 - Get Recent System Errors

Please select a task
"@
$r=read-host $p

Switch ($r) {

1 {Get-service | where {$_.status -eq "Running"} }
2 {Get-process}
3 {Get-eventlog system -EntryType Error -Newest 5}
Default {Write-Host "Invalid choice: $r" -fore Yellow}

}

If I run a script with this, here's the output:

\\\\\\\\
  Menu
////////

  1 - Get Running Services
  2 - Get Processes
  3 - Get Recent System Errors

Please select a task: 3

  Index Time         EntryType Source               InstanceID Message
  ----- ----         --------- ------               ---------- -------
1513191 Jan 31 21:16 Error     Microsoft-Windows...         12 The pla...
1513189 Jan 31 23:42 Error     SCardSvr                    616 The des...
1513188 Jan 31 23:42 Error     SCardSvr                    612 The des...
1513187 Jan 31 23:42 Error     SCardSvr                    610 The des...
1513186 Jan 31 23:42 Error     SCardSvr                    610 The des...

The here string provides a much easier way to construct a formatted multiline string. And because it is still a string, normal quoting rules apply:

PS C:\> $h=@"
>> User=$env:username
>>   Process Count=$((get-process).Count)
>>   OS=$((Get-WmiObject win32_operatingsystem).Caption)
>> "@
>>
PS C:\> $h
User=Jeff
  Process Count=131
  OS=Microsoft Windows 7 Ultimate

So the next time you need a complex string, don't trip over a bunch of escape characters and concatenation, start here.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular