357

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"
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
askedApr 9, 2010 at 14:11
asgerhallas's user avatar
2

8 Answers8

547

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.

Myrddin Emrys's user avatar
Myrddin Emrys
44.5k12 gold badges44 silver badges60 bronze badges
answeredApr 9, 2010 at 14:17
Colin Pickard's user avatar
Sign up to request clarification or add additional context in comments.

8 Comments

This seems to break command history (up arrow) functionality; as each line shows up as a separate command. Is there a way around this?
If you're running powershell 3 or higher, see github.com/lzybkr/psreadline - history traversal is fixed for multiline statements.
The space in front of the back-tick is required #learned-the-hard-way
@josh-graham And there should NOT be any space (or inline comment) AFTER the back-tick. #learned-the-hard-way
Backticks are brittle (as above comments state) and hard to find when parsing or reviewing a file. @StevenPenny 's answer is better if you want easier to debug code.
|
100

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 @params

Microsoft Docs:About Splatting

TechNet Magazine 2011:Windows PowerShell: Splatting

Looks like it works with Powershell 2.0 and up

spottedmahn's user avatar
spottedmahn
16.2k21 gold badges124 silver badges204 bronze badges
answeredJun 19, 2014 at 17:53
BJHop's user avatar

5 Comments

This is wonderful! AND you can add parameters like this:$params.add('name','Bob Newhart')ramblingcookiemonster.wordpress.com/2014/12/01/…
The semicolons are ok but superfluous. Only required if there are multiple values per line.
This doesn't work for normal shell command, mainly for PowerShell commandlets
This should be the new accepted answer. Splatting is a very standard thing to do nowadays.
How does it work with the example provided in the question?
51

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”

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredAug 23, 2012 at 17:13
bgmCoder's user avatar

3 Comments

Neat trick, though if I have a variable $... it seems to not work. I get "the character is not allowed after a here string header..."
I don't think you can break a variable name, just a string.
Can you break the string in the source without having the new line characters figure in the result? Like an argument list.
29

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 $destArg
answeredAug 1, 2013 at 17:44
Aaron Jensen's user avatar

1 Comment

I like variable names over other suggestions because it's possibly the most readable option for non powershell experts. If I saw a tutorial/set-up guide that used splatting I would be totally lost at what is going on without a sub-tutorial on splatting. Likewise, backticks seem fragile and probably less well known than simple tried and true PS variables.
19

If you have a function:

$function:foo | % Invoke @(  'bar'  'directory'  $true)

If you have acmdlet:

[PSCustomObject] @{  Path  = 'bar'  Type  = 'directory'  Force = $true} | New-Item

If you have an application:

{foo.exe @Args} | % Invoke @(  'bar'  'directory'  $true)

Or

icm {foo.exe @Args} -Args @(  'bar'  'directory'  $true)
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredNov 11, 2014 at 23:42
Zombo's user avatar

Comments

8

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"
answeredFeb 3, 2016 at 15:05
Bad's user avatar

Comments

6

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"
answeredNov 18, 2019 at 16:48
Kirill Yunussov's user avatar

2 Comments

This is so ugly! :) Still no better solution in 2023?
this seems better but in the same vain:$blah = ("blah" + " blah") @Lorlin
5

Splat 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 @params

Note: The file array contains information that will affect how the spreadsheet is populated.

answeredFeb 27, 2020 at 22:06
Mark's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.