{"id":86319,"date":"2019-09-11T01:00:23","date_gmt":"2019-09-11T09:00:23","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=86319"},"modified":"2019-09-06T08:41:34","modified_gmt":"2019-09-06T16:41:34","slug":"maximizing-the-power-of-here-string-in-powershell-for-configuration-data","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/maximizing-the-power-of-here-string-in-powershell-for-configuration-data\/","title":{"rendered":"Maximizing the Power of Here-String in PowerShell for Configuration Data"},"content":{"rendered":"<p><strong>Summary<\/strong>: Discovering some of the awesome and neat ways to leverage a Here-String in a Script or function<\/p>\n<p>Q: Hey, Doctor Scripto!<\/p>\n<p>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.<\/p>\n<p>\u2014AB<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>If you&#8217;ve never encountered a Here-String before, this is an example.<\/p>\n<pre class=\"lang:ps decode:true \">$Message=@'\r\nHello people, this is Doctor Scripto\r\nWelcome to the Scripting Blog\r\nToday we're showing you a Here-String.\r\n'@<\/pre>\n<p>&nbsp;<\/p>\n<p>When you examine the properties of <strong>$Message<\/strong> you&#8217;ll see it not an Array but one long string (including carriage returns and linefeed characters within)<\/p>\n<p><img decoding=\"async\" width=\"1994\" height=\"803\" class=\"wp-image-86320\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-6.png\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-6.png 1994w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-6-300x121.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-6-768x309.png 768w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-6-1024x412.png 1024w\" sizes=\"(max-width: 1994px) 100vw, 1994px\" \/><\/p>\n<p>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 &#8220;What You See Is What You Get&#8221;.<\/p>\n<p>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.<\/p>\n<p>Another example of a Here-String is when you enclose it with Double Quotes (&#8220;). When it&#8217;s done this way the content is now Evaluated in the parser before it is consumed by the rest the script. Here&#8217;s a simple example such as new user Letter<\/p>\n<pre class=\"lang:ps decode:true\">$FirstName='Doctor'\r\n$LastName='Scripto'\r\n$UserID=$Firstname+'.'+$Lastname\r\n$PhoneNumber='425-555-1212'\r\n$Office='Seattle'\r\n\r\n$TemporaryPassword=[System.Web.Security.Membership]::GeneratePassword(10,0)\r\n\r\n$WelcomeLetter=@\"\r\nHello and welcome to Contoso $FirstName !\r\n\r\nYour user id on the system will be $UserID with a Temporary Password of $TemporaryPassword\r\nand your home Office is in $Office. You can be reached at $PhoneNumber.\r\n\r\nIf you have any issues with your setup you can call 425-555-1111 in order to reach the Help desk.\r\n\r\nThanks!\r\nYour Friendly Neighborhood It Department\r\n\"@<\/pre>\n<p>If you ran this and went to display the object <strong>$WelcomeLetter<\/strong> , instead of seeing Variables it would all expand to real values like this.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-86322 size-full alignnone\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/Example.png\" alt=\"\" width=\"2685\" height=\"466\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/Example.png 2685w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/Example-300x52.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/Example-768x133.png 768w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/Example-1024x178.png 1024w\" sizes=\"(max-width: 2685px) 100vw, 2685px\" \/><\/p>\n<p>Again something that&#8217;s a bit easier to edit and view. But how about an array of information? Normally I would read an array in this fashion.<\/p>\n<pre class=\"lang:ps decode:true \">[system.array]$Somelist='Value1','Value2','Value3','Value4'<\/pre>\n<p>But for a person who might NOT be a PowerShell Guru, wouldn&#8217;t it be cool to place that array in this fashion?<\/p>\n<pre class=\"lang:ps decode:true \">$SomeListToUse=@'\r\nValue1\r\nValue2\r\nValue3\r\nValue4\r\n'@<\/pre>\n<p>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).<\/p>\n<pre class=\"lang:ps decode:true \">$SomeList=$SomeListToUse -replace \"`n\" , ''<\/pre>\n<p>Then we can use split method by targeting the &#8220;CarriageReturn&#8221; (ASCII 13) at the termination of the string to convert it to an Array.<\/p>\n<pre class=\"lang:ps decode:true \">$SomeList=$SomeList.split(\"`r\")<\/pre>\n<p>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&#8217;t see a linefeed anywhere in it)<\/p>\n<p>In which case our conversion would look like this<\/p>\n<pre class=\"lang:ps decode:true \">$SomeList=$SomeListToUse.split(\"`r\")<\/pre>\n<p>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.<\/p>\n<pre class=\"lang:ps decode:true \">$CSVListTemp=@'\r\nUserID,Domain\r\nDScripto,Contoso\r\nJSmith,Fabrikam\r\n'@\r\n\r\n$CSVList=$CSVListTemp -replace \"`n\" , ''\r\n$CSVList=$CSVList.split(\"`r\")\r\n$CSVData=$CSVList | ConvertFrom-CSV\r\n<\/pre>\n<p>Of course these are just examples of ways you COULD use a Here-String within a script, not how you MUST.<\/p>\n<p>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<\/p>\n<p>AB that is all there is to using Here-String for the moment.\u00a0 But who knows?\u00a0 \u00a0They may be more to discover next Week!<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>Your good friend, Doctor Scripto<\/p>\n<p>PowerShell, Doctor Scripto, Sean Kearney<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1739,1738,685,683,688],"tags":[1740,377,154],"class_list":["post-86319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-doctor-scripto","category-powershell","category-scripting-techniques","category-text-files","category-text-manipulation","tag-doctor-scripto","tag-powershell","tag-sean-kearney"],"acf":[],"blog_post_summary":"<p>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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=86319"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86319\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=86319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=86319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=86319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}