DEV Community

loading...
Cover image for PowerShell Pro Tip - Get an Elevated Session

PowerShell Pro Tip - Get an Elevated Session

sharpninja profile image The Sharp Ninja ・2 min read

Sometimes You Need to Turn it Up to 11

We've all been there... we're working fine in our CLI with normal privileges like good citizens, then we need to install something or change a setting. We're typing away when BAM Access Denied.

Normally we grumble to ourselves about if only Windows had sudo as we reach for the mouse to right-click the Windows Terminal icon pinned to our task bar, select "Run As Administrator" and continue working after navigating to wherever it was you were at to begin with. It doesn't seem like a big deal, but it really is.

So today I'm going to give you a Pro-Tip for your $Profile. Open your $Profile script and add this to the end:

function Elevate-Command {
    param(
        [switch]$Wait = $false,
        [switch]$Verbose = $false
    )

    $cmd = Get-Command wt.exe
    $cmdparams = "new-tab", "-d" 
    $cmdparams += Get-Location

    switch($PSVersionTable.PSEdition){
        "Core" { $pwsh = Get-Command pwsh }
        default { $pwsh = Get-Command powershell.exe }
    }

    if ($args) {
        $parameters = "-NoExit", "-Command";

        [string[]]$a = ($args | foreach-object -process { $_.ToString()});

        [System.Collections.Generic.List[string]]$Arguments = `
            New-Object System.Collections.Generic.List[string];

        $Arguments.AddRange($a);

        if($args[0] -imatch "pwsh"){
            $pwsh = Get-Command pwsh
            $Arguments.RemoveAt(0);
        } elseif($args[0] -imatch "powershell"){
            $pwsh = Get-Command powershell.exe
            $Arguments.RemoveAt(0);
        } elseif(($args[0] -imatch "cmd") -or ($args[0] -imatch ".*\.bat")){
            $parameters = "/K"
            $pwsh = Get-Command cmd.exe

            if($args[0] -imatch "cmd") {
                $parameters = $null;
                $Arguments.RemoveAt(0);
            } 
        }

        [string[]]$arg = $cmdparams
        $arg += $pwsh.Source
        $arg += $parameters
        $arg += $Arguments

        $ar = [string]::Join(" ", $arg)
    } else {
        $ar = $pwsh.Source
    }    

    Start-Process -Verbose:$Verbose -Verb RunAs -Wait:$Wait -FilePath $cmd.Source -ArgumentList $ar
}

function Elevate-CommandAndWait {
    param(
        [switch]$Verbose = $false
    )

    Elevate-Command -Verbose:$Verbose -Wait -Arguments $args;
}

set-alias -Name sudo -Value Elevate-Command -PassThru -Scope Global;
set-alias -Name sudow -Value Elevate-CommandAndWait -PassThru -Scope Global;

Save your profile and restart PowerShell. Now type sudo and hit enter. Bam! A new elevated PowerShell session, and it's in the correct folder. You can use it with commands too, such as sudo choco install dotnetcore.

It also has some smarts built in, such as knowing when you are executing a batch file and using cmd.exe instead of PowerShell. It also knows if you are using Windows PowerShell or Powershell Core and calls the correct one.

Discussion (0)

pic
Editor guide