0

I am working on a batch file that can run a PowerShell script when the batch file is double clicked. I am having difficulty getting the PowerShell script to do anything when activated by the batch file. The PowerShell script works perfectly when I test out the ps1 file. But once I call from it the batch file it doesn't seem to work.

Batch file:

@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%FileNotifyscript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";

enter image description here

0

It is an escaping issue,

  • cmd will interpret the inner double quotes as ending the first one
    as the single quotes have a special meaning only to PowerShell.
  • use a backslash to escape one \" double quote

:: Q:\Test\2019\04\22\SU_1428283.cmd
@ECHO OFF
SET "PowerShellScriptPath=%~dp0FileNotifyscript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command ^ 
 "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File \"%PowerShellScriptPath%\"' -Verb RunAs}"
pause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.