Running a command as Administrator using PowerShell?

后端 未结 26 3252
粉色の甜心
粉色の甜心 2020-11-22 09:41

You know how if you\'re the administrative user of a system and you can just right click say, a batch script and run it as Administrator without entering the administrator p

26条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 10:22

    Self elevating PowerShell script

    Windows 8.1 / PowerShell 4.0 +

    One line :)

    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
    
    # Your script here
    

提交回复
热议问题