Category Archives: Powershell

Run PowerShell as admin from command line

Life is really simple, but we insist on making it complicated.

Confucius

Windows Terminal is a great tool for having multiple profiles for different CLI tools. Starting terminal from the start menu or from launcher (like PowerToys Run) can be quick and efficient. In some cases, you will need admin privileges to perform a command. In Windows holding CTRL+SHIFT and pressing the left mouse button does the trick. In launcher that can be even easier by configuring your own key binding. But what if you are already in the terminal window and just want to open it as admin from there?

Opening PowerShell from command line

PowerShell has an alias called start which refers to Start-Process cmdlet and usage is quite simple – just start alias and then program we want to execute. As an example:

start powershell.exe

This simple command will start Windows PowerShell in the current directory.

Great, right?

But we need admin privileges. How to do start the program to run as admin?

Looking closely at Start-Process we noticed it has a Verb option.

Start alias help

After reading the official docs we do have an example to see what we can leverage as verbs with PowerShell:

$startExe = New-Object System.Diagnostics.ProcessStartInfo -Args PowerShell.exe
$startExe.verbs

The result gives us 3 options to choose from:

Possible Verbs to use with Start-Process

Taking advantage of the verb RunAs we can run the process as specific user. But how to define administrator?

Long story short, we can just use RunAs without parameter, and we will get famous popups from Windows saying to select elevated privileges under which the process will execute:

start PowerShell.exe -Verb RunAs

This will run the new Window Powershell window with elevated permission.

Easy, right?

Making RunAs useful in PowerShell

Writing the whole procedure manually can be cumbersome and time consuming. Having an alias to just say goadmin would be more efficient and useful.

Let us take advantage of that. We already discussed how we can create aliases to handle functions calls (if you don’t know what I am talking about, check this) and we can take advantage of that:

Set-Alias -Name goadmin -Value GoAdminFunc

We need to write the function GoAdminFunc. We can create a method and add upper commands to the statement body:

Function GoAdminFunc(){
    Start-Process -Verb RunAs PowerShell
}

This is great, but this executes everytime. What if we are already signed in as admin? We don’t need to get the popup dialog each time.

To solve that challenge, we can take advantage of .NET library support to check if we are in a specific role. With simple IF we can execute new process if we are in Administrator role. IsInRole is the method to be used based – on docs here. With that we can shape the func a bit to take that into consideration:

Function GoAdminFunc(){
   If (!([Security.Principal.WindowsPrincipal]     [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.Windo  wsBuiltInRole]'Administrator'))
  {
      Start-Process -Verb RunAs PowerShell
      Exit
  }
}

With that we can then easily run goadmin command inside PowerShell and we will get runas dialog to choose from. Exit command on the end closes the window to reduce clutter with open terminals.

GoAdmin function call

Windows Terminal option

If you are using Windows Terminal as your GUI for executing CLI commands, you can easily run WT with preferred option.

Function GoAdminFunc(){
   If (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator'))
   {
     Start-Process -Verb RunAs wt.exe '-p "PowerShell"'
     Exit
    }
}

-p is the name of the the profile you have specified in Windows Terminal. More about that here.

Conclusion

Aliases are a wonderful way to bend PowerShell to your workflow. People tend to type more and spend keystroke on repetitive tasks. Thanks to PowerShell extensibility we can focus on the task at hand and leave system operations to PowerShell. Hour saved on repetitive tasks is an hour well spent somewhere else.