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.

Compo
  • 36,585
  • 5
  • 27
  • 39
Ayaz49
  • 325
  • 2
  • 4
  • 18

1 Answers1

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.

Ayaz49
  • 325
  • 2
  • 4
  • 18