Thursday, April 18, 2019

Run Powershell with elevate without prompt - verb runas start-process


Based on https://ss64.com/ps/syntax-elevate.html this:


Create a script as :

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))

{

  # Relaunch as an elevated process:

  Start-Process powershell.exe   "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs

  exit

}

# Now running elevated so launch the script:

& $scriptPath + "\test.ps1" "Long Argument 1" "Long Argument 2"

To disable the UAC pop up windows

REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f

No comments:

Post a Comment