11

I have a batch script that looks like this:

Test.bat

@echo off

:: Starts a PowerShell session that starts a PowerShell process with administrator privileges
powershell -noprofile -command "&{$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;$process.WaitForExit();}"

I would like to split up the code inside the "&{ ... }" over multiple line for readability.

This post says that using a trailing backtick should do the trick, but when I write:

powershell -noprofile -command "&{`
    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
    $process.WaitForExit();`
}"

...that is, I end each line with a trailing backtick, then I get the following error:

Incomplete string token.
At line:1 char:4
+ &{` <<<<
    + CategoryInfo          : ParserError: (`:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : IncompleteString

'$process' is not recognized as an internal or external command,
operable program or batch file.
'$process.WaitForExit' is not recognized as an internal or external command,
operable program or batch file.
'}"' is not recognized as an internal or external command,
operable program or batch file.

What am I doing wrong?

SOLUTION:

powershell -noprofile -command "&{"^
    "$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
    "$process.WaitForExit();"^
    "}"
Community
  • 1
  • 1
Janne Beate Bakeng
  • 187
  • 1
  • 4
  • 10

2 Answers2

9

You may try the caret ^ to indicate that the current line continues on the next one:

powershell -noprofile -command "&{"^
 "$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;"^
 "$process.WaitForExit();"^
 "}"

Please note also the leading space as well as the closing and opening " on each line.

See also Long commands split over multiple lines in Windows Vista batch (.bat) file.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
marapet
  • 49,690
  • 10
  • 152
  • 168
  • That results in: "Missing closing '}' in statement block. At line:1 char:4 + &{^ <<<< + CategoryInfo : ParserError: (CloseBraceToken:TokenId) [], ParentContainsErrorRecord Exception + FullyQualifiedErrorId : MissingEndCurlyBrace '$process' is not recognized as an internal or external command, operable program or batch file." – Janne Beate Bakeng Jun 20 '12 at 12:19
  • Try adding a space at the beginning of the following lines. – marapet Jun 20 '12 at 12:21
  • Now it works... on top of the leading space, double-quotes have to be closed before the end of line and reopened on the next line. – marapet Jun 20 '12 at 12:33
  • I got it to work, you almost had it, just need to remove the trailing backticks. :) Thanks! – Janne Beate Bakeng Jun 20 '12 at 12:43
  • I did remove them in my example... after having spotted them :-) – marapet Jun 20 '12 at 12:44
1

Try the following. I think this will help.

powershell -noprofile -command ""&{^
    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb
    RunAs -PassThru;^
    $process.WaitForExit();^
}""
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
singh
  • 429
  • 1
  • 3
  • 19