I have a batch script that looks like this:
Test.bat
@echo off:: Starts a PowerShell session that starts a PowerShell process with administrator privilegespowershell -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();"^ "}"2 Answers2
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 alsoLong commands split over multiple lines in Windows Vista batch (.bat) file.
5 Comments
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();^}""Comments
Explore related questions
See similar questions with these tags.
