1

Problem:

I want to call my PowerShell script with elevated privileges in a non-administrator command prompt. When I manually open the Command Prompt with 'Run as Administrator' and enter the line:

powershell -ExecutionPolicy Unrestricted -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File example.ps1 param1'"

My script PowerShell script runs just fine.

However, when I run it in a non-administrator command prompt, I see a PowerShell window appear for a split second and exits.

Any ideas on how to resolve this issue?

Tries

  • Same thing happens, the window opens for a split second and then exits

Run Powershell script via batch file with elevated privileges

runas.exe /netonly /noprofile /user:domainadm@domain "powershell.exe -
noprofile -File "C:\Users\...\Desktop\Powershell_scripts\New-
ADuser\.ps1" -verb RunAs"
  • Same thing happens here as well, the window opens for a split second and then exits

Run a Powershell script from Batch file with elevated privileges?

powershell -Command "&{ Start-Process powershell -ArgumentList '-File example.ps1 param1' -Verb RunAs}"
  • Same issue, the window opens for a split second and then exits

Run a powershell script from cmd with elevated privileges and passing parameters

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "example.ps1 param1"'"

CONTEXT: My PowerShell script is for downloading a NSIS Installer specified at param1

EDIT: Script code:

$param1 = $args[0]
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

$installer = Start-Process -FilePath $param1 -PassThru
$installerWindowName = "Installer Setup"

while (-not ($installer.MainWindowTitle -match "$installerWindowName")) {
    Start-Sleep -Milliseconds 100
    $installer.Refresh()
}

1 Answer 1

0
  • On Windows, an elevated process launched from a non-elevated console invariably runs in a new window, which closes automatically when the elevated process exits.

    • For troubleshooting, you can place -NoExit before the -Command argument, which keeps the elevated PowerShell session open until you exit it manually, allowing you to inspect any errors that may have occurred.
  • In Windows PowerShell (whose CLI is powershell.exe), an elevated process launches with $env:Windir\System32 as its working directory (fortunately, PowerShell (Core) 7+ now preserves the caller's working directory).

    • Therefore, using a relative path such as example.ps1 does not work, and you should pass a full path instead.
      • If your script relies on the working directory to be the same as the caller's, more work would be needed.
# Note the `"$PWD\example.ps1`" part
powershell -ExecutionPolicy Bypass -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File `"$PWD\example.ps1`" param1'"
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.