{"id":76011,"date":"2016-01-02T00:01:00","date_gmt":"2016-01-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/01\/02\/learn-easier-way-to-build-command-for-powershell-exe\/"},"modified":"2019-02-18T09:20:27","modified_gmt":"2019-02-18T16:20:27","slug":"learn-easier-way-to-build-command-for-powershell-exe","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-easier-way-to-build-command-for-powershell-exe\/","title":{"rendered":"Learn Easier Way to Build Command for PowerShell.exe"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to parse a here-string with Windows PowerShell.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here to show you a neat little trick.<\/p>\n<p>Anyone who has ever scheduled a PowerShell task knows that if you want to launch a cmdlet directly in PowerShell, you would launch it like this:<\/p>\n<p style=\"margin-left:30px\">PowerShell.exe &ndash;ExecutionPolicy Bypass &ndash;command &lsquo;Get-ChildItem&rsquo;<\/p>\n<p>No challenge there. It also does not end there. You can add some down-right complex stuff in there, for example:<\/p>\n<p style=\"margin-left:30px\">PowerShell.exe &ndash;executionpolicy Bypass &ndash;command &lsquo;Set-ExecutionPolicy Bypass;Import-Module DeployImage; New-WindowsPeImage&rsquo;<\/p>\n<p>This is an example of something I was playing with in the <b>DeployImage<\/b> module when launching PowerShell automatically.<\/p>\n<p>Then again, in many cases, a script would be much easier to run if you knew where the script actually was. The challenge I ran into was trying to put together really complex command while typing it in the parameter.<\/p>\n<p>Then it dawned on me, &ldquo;Why not write it as a script to make sure it works?&rdquo; Yeah, that was pretty obvious. But how would I flip it back to a command for the PowerShell.exe? This is where a here-string is perfect!<\/p>\n<p>Here I have an example of something that I was actually doing. I needed to take the following script and flip it to a command in the PowerShell.exe.<\/p>\n<p style=\"margin-left:30px\">Set-ExecutionPolicy -executionpolicy Bypass<\/p>\n<p style=\"margin-left:30px\">$USBDisk=(Get-Disk | Where-Object { $_.BusType -eq &#8216;USB&#8217; -and &#8216;$_.IsActive&#8217; })<\/p>\n<p style=\"margin-left:30px\">$DriveLetter=($USBDisk | Get-Partition).DriveLetter<\/p>\n<p style=\"margin-left:30px\">Set-Location ($DriveLetter+&#8217;:\\DeployImage\\&#8217;)<\/p>\n<p style=\"margin-left:30px\">Import-Module ($DriveLetter+&#8217;:\\DeployImage\\DeployImage.Psd1)&#8217;<\/p>\n<p>I then stored it as a here-string:<\/p>\n<p style=\"margin-left:30px\">$PowerShellScript=@&#8217;<\/p>\n<p style=\"margin-left:30px\">Set-ExecutionPolicy -executionpolicy Bypass<\/p>\n<p style=\"margin-left:30px\">$USBDisk=(Get-Disk | Where-Object { $_.BusType -eq &#8216;USB&#8217; -and &#8216;$_.IsActive&#8217; })<\/p>\n<p style=\"margin-left:30px\">$DriveLetter=($USBDisk | Get-Partition).DriveLetter<\/p>\n<p style=\"margin-left:30px\">Set-Location ($DriveLetter+&#8217;:\\DeployImage\\&#8217;)<\/p>\n<p style=\"margin-left:30px\">Import-Module ($DriveLetter+&#8217;:\\DeployImage\\DeployImage.Psd1)&#8217;<\/p>\n<p style=\"margin-left:30px\">&#8216;@<\/p>\n<p>Now all we need is to find where each line terminates and switch it with a semicolon.<\/p>\n<p>We can examine each byte in the here-string in the following manner:<\/p>\n<p style=\"margin-left:30px\">[byte][char]($PowerShellScript)[10]<\/p>\n<p>We would eventually notice that two characters at the end of each line are:<\/p>\n<ul>\n<li>Ascii 13 (Return)<\/li>\n<li>Ascii 10 (Linefeed)<\/li>\n<\/ul>\n<p>Although we can&rsquo;t see or type these, we can represent these as a string in the following manner:<\/p>\n<p style=\"margin-left:30px\">$CRLF=[char][byte]13 + [char][byte]10<\/p>\n<p>Armed with this, we can now do something really neat&mdash;use the <b>Replace<\/b> method and switch out each carriage return\/linefeed with a semicolon:<\/p>\n<p style=\"margin-left:30px\">$PowerShellCommand=$PowerShellScript.replace($CRLF,&rsquo;;&rsquo;)<\/p>\n<p>You can now use this in whatever fashion suits you&mdash;even if that means dropping it into the clipboard!<\/p>\n<p>Have fun and enjoy!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy, Cloud and Datacenter Management MVP<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to parse a here-string with Windows PowerShell. Honorary Scripting Guy, Sean Kearney, is here to show you a neat little trick. Anyone who has ever scheduled a PowerShell task knows that if you want to launch a cmdlet directly in PowerShell, you would launch it like this: PowerShell.exe &ndash;ExecutionPolicy Bypass &ndash;command &lsquo;Get-ChildItem&rsquo; [&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":[1],"tags":[56,154,61,45],"class_list":["post-76011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to parse a here-string with Windows PowerShell. Honorary Scripting Guy, Sean Kearney, is here to show you a neat little trick. Anyone who has ever scheduled a PowerShell task knows that if you want to launch a cmdlet directly in PowerShell, you would launch it like this: PowerShell.exe &ndash;ExecutionPolicy Bypass &ndash;command &lsquo;Get-ChildItem&rsquo; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76011","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=76011"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76011\/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=76011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=76011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=76011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}