How do you take a command like the following in PowerShell and split it across multiple lines?
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"- See also ...stackoverflow.com/questions/3235850/…SteveC– SteveC2013-11-21 15:42:46 +00:00CommentedNov 21, 2013 at 15:42
- 1Possible duplicate ofHow to enter a multi-line command?Michael Freidgeim– Michael Freidgeim2017-06-23 05:28:57 +00:00CommentedJun 23, 2017 at 5:28
8 Answers8
Trailing backtick character, i.e.,
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `-verb:sync `-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `-dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"White space matters. The required format isSpace`Enter.
8 Comments
Another method for cleaner argument passing would besplatting.
Define your parameters and values as a hashtable like this:
$params = @{ 'class' = 'Win32_BIOS'; 'computername'='SERVER-R2'; 'filter'='drivetype=3'; 'credential'='Administrator' }And then call your commandlet like this:
Get-WmiObject @paramsMicrosoft Docs:About Splatting
TechNet Magazine 2011:Windows PowerShell: Splatting
5 Comments
$params.add('name','Bob Newhart')ramblingcookiemonster.wordpress.com/2014/12/01/…Ah, and if you have a very long string that you want to break up, say of HTML, you can do it by putting a@ on each side of the outer" - like this:
$mystring = @"Bobwentto townto buya fatpig."@You get exactly this:
Bobwentto townto buya fatpig.And if you are usingNotepad++, it will even highlight correctly as a string block.
Now, if you wanted that string to contain double quotes, too, just add them in, like this:
$myvar = "Site"$mystring = @"<a href="http://somewhere.com/somelocation">Bob's $myvar</a>"@You would get exactly this:
<a href="http://somewhere.com/somelocation">Bob's Site</a>However, if you use double-quotes in that @-string like that, Notepad++ doesn't realize that and will switch out the syntax colouring as if it were not quoted or quoted, depending on the case.
And what's better is this: anywhere you insert a $variable, it DOES get interpreted! (If you need the dollar sign in the text, you escape it with a tick mark like this: ``$not-a-variable`.)
NOTICE! If you don't put the final"@ at thevery start of the line, it will fail. It took me an hour to figure out that I could not indent that in my code!
Here isMSDN on the subject:Using Windows PowerShell “Here-Strings”
3 Comments
You can use the backtick operator:
& "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" ` -verb:sync ` -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" ` -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"That's still a little too long for my taste, so I'd use some well-named variables:
$msdeployPath = "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"$verbArg = '-verb:sync'$sourceArg = '-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web"'$destArg = '-dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"'& $msdeployPath $verbArg $sourceArg $destArg1 Comment
If you have a function:
$function:foo | % Invoke @( 'bar' 'directory' $true)If you have acmdlet:
[PSCustomObject] @{ Path = 'bar' Type = 'directory' Force = $true} | New-ItemIf you have an application:
{foo.exe @Args} | % Invoke @( 'bar' 'directory' $true)Or
icm {foo.exe @Args} -Args @( 'bar' 'directory' $true)Comments
In PowerShell 5 and PowerShell 5 ISE, it is also possible to use justShift +Enter for multiline editing (instead of standard backticks` at the end of each line):
PS> &"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" # Shift+Enter>>> -verb:sync # Shift+Enter>>> -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" # Shift+Enter>>> -dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"Comments
Another way to break a string across multiple lines is to put an empty expression in the middle of the string, and break it across lines:
sample string:
"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"
broken across lines:
"stackoverflow stackoverflow $()stackoverflow stack$()overflow stackoverflow"2 Comments
$blah = ("blah" + " blah") @LorlinSplat Method with Calculations
If you choose splat method, beware calculations that are made using other parameters. In practice, sometimes I have to set variables first then create the hash table. Also, the format doesn't require single quotes around the key value or the semi-colon (as mentioned above).
Example of a call to a function that creates an Excel spreadsheet$title = "Cut-off File Processing on $start_date_long_str"$title_row = 1$header_row = 2$data_row_start = 3$data_row_end = $($data_row_start + $($file_info_array.Count) - 1)# use parameter hash table to make code more readable$params = @{ title = $title title_row = $title_row header_row = $header_row data_row_start = $data_row_start data_row_end = $data_row_end}$xl_wksht = Create-Excel-Spreadsheet @paramsNote: The file array contains information that will affect how the spreadsheet is populated.
Comments
Explore related questions
See similar questions with these tags.









