Sudo in Windows with gsudo
One of the things I have always been frustrated is the lack of a sudo
command in PowerShell or CMD on Windows. Simply, sudo
is a *nix command the elevates the command you're running to run with administrative privileges.
On Windows, if you want to do that, the shortest method to do so is to use the command Start-Process powershell -Verb runas
So for example, if you want to install a PowerShell module to All Users, the command would look like:
Start-Process powershell -ArgumentList "-Command", "Install-Module SyncroRMM -Scope AllUsers" -Verb RunAs
Enter gsudo
gsudo is a small utility that gives you sudo
like abilities in Windows. Just install it with winget install geradog.gsudo
and you're ready to go. Now the command to install a module from earlier is simply:
gsudo Install-Module SyncroRMM -Scope AllUsers
Bang Bang Commands
gsudo
also includes a PowerShell module that can be used to enable !!
(bang bang) commands. In Linux, !!
replaces itself with the last command you ran so you can do this:
> useradd foo
useradd: Permission denied.
> sudo !!
sudo useradd foo
With gsudo
, run Import-Module -Name gsudoModule
and then you can do the same:
> Install-Module SyncroRMM -Scope AllUsers
Install-Module: Administrator rights are required to install modules
> gsudo !!
VERBOSE: Elevating Command: 'install-module ExchangeOnlineManagement -Scope AllUsers'
To make it even easier, open your PowerShell profile with notepad $PROFILE
and add the following to the end:
if (Get-Module -ListAvailable -Name gsudoModule) {
Import-Module -Name gsudoModule
}
Now the !!
will always be available in your PowerShell console.