Running a command as Administrator using PowerShell?

后端 未结 26 2854
粉色の甜心
粉色の甜心 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:07

    You can easily add some registry entries to get a "Run as administrator" context menu for .ps1 files:

    New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell\runas\command" `
    -Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit "%1"'
    

    (updated to a simpler script from @Shay)

    Basically at HKCR:\Microsoft.PowershellScript.1\Shell\runas\command set the default value to invoke the script using Powershell.

    0 讨论(0)
  • 2020-11-22 10:10

    Adding my 2 cents. My simple version based on net session which works all the time so far in Windows 7 / Windows 10. Why over complicate it?

    if (!(net session)) {$path =  "& '" + $myinvocation.mycommand.definition + "'" ; Start-Process powershell -Verb runAs -ArgumentList $path ; exit}
    

    just add to the top of the script and it will run as administrator.

    0 讨论(0)
  • 2020-11-22 10:10

    On top of Shay Levy's answer, follow the below setup (just once)

    1. Start a PowerShell with Administrator rights.
    2. Follow Stack Overflow question PowerShell says “execution of scripts is disabled on this system.”.
    3. Put your .ps1 file in any of the PATH folders, for example. Windows\System32 folder

    After the setup:

    1. Press Win + R
    2. Invoke powershell Start-Process powershell -Verb runAs <ps1_file>

    You can now run everything in just one command line. The above works on Windows 8 Basic 64-bit.

    0 讨论(0)
  • 2020-11-22 10:10

    To append the output of the command to a text filename which includes the current date you can do something like this:

    $winupdfile = 'Windows-Update-' + $(get-date -f MM-dd-yyyy) + '.txt'
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -Command `"Get-WUInstall -AcceptAll | Out-File $env:USERPROFILE\$winupdfile -Append`"" -Verb RunAs; exit } else { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -Command `"Get-WUInstall -AcceptAll | Out-File $env:USERPROFILE\$winupdfile -Append`""; exit }
    
    0 讨论(0)
  • 2020-11-22 10:13

    Here's a self-elevating snippet for Powershell scripts which preserves the working directory:

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

    Preserving the working directory is important for scripts that perform path-relative operations. Almost all of the other answers do not preserve this path, which can cause unexpected errors in the rest of the script.

    If you'd rather not use a self-elevating script/snippet, and instead just want an easy way to launch a script as adminstrator (eg. from the Explorer context-menu), see my other answer here: https://stackoverflow.com/a/57033941/2441655

    0 讨论(0)
  • 2020-11-22 10:14

    A number of the answers here are close, but a little more work than needed.

    Create a shortcut to your script and configure it to "Run as Administrator":

    • Create the shortcut.
    • Right-click shortcut and open Properties...
    • Edit Target from <script-path> to powershell <script-path>
    • Click Advanced... and enable Run as administrator
    0 讨论(0)
提交回复
热议问题