0

I have a PowerShell script that needs to be executed using Windows' Batch Script as Administrator. The script is running without arguments, however when I pass arguments, the PowerShell window pops up and closes instantly. Here is what I have tried.

Batch script

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile - 
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1" -FolderPath \"test"'   -Verb 
RunAs"

Powershell

param
(
 [string]$FolderPath ='D:\batch_scripting'
 )

echo $FolderPath
pause

I will add further functionalities in these scripts later. But I have to figure out this first. The script can be executed if -FolderPath argument is not passed.

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile - 
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1"' -Verb 
RunAs"

I have also gone through following questions but this does not work for me.

6
  • stackoverflow.com/questions/72563402/… Commented Sep 16, 2022 at 8:20
  • stackoverflow.com/questions/69285963/… Commented Sep 16, 2022 at 8:20
  • Use the -ArgumentList switch to pass arguments. Commented Sep 16, 2022 at 10:14
  • Your examples are incorrect because they should be on a single line, not over three. Also, you do not need the first -ExecutionPolicy Bypass either. Commented Sep 16, 2022 at 11:34
  • tried -ArgumentList, same problem. Commented Sep 17, 2022 at 14:14

1 Answer 1

0

I found the solution, although it's weird. When you execute the powershell script as Administrator, trailing "/" must be added to the path of the script.

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell 'NoProfile - ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1\"' -Verb RunAs"

It works fine now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy 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.