Peter Girnus

View Original

Sudo in PowerShell: Get Administrator Privileges With gsudo

Are you exhausted from the repetitive process of right-clicking to execute "Run as administrator" when you need an elevated PowerShell terminal? Imagine if there was an effortless method to elevate your current Windows shell. While the Linux sudo command is widely recognized, you might not be aware of its incredible Microsoft Windows counterpart: gsudo! In this guide, I'll walk you through a few simple steps to install and set up this fantastic Windows application for seamless terminal elevation to the Administrator account.

Installing gsudo With WinGet

We can simply install the gsudo package using the Windows Package Manager (winget). If you’re unsure if you have winget installed I cover this awesome Windows tool in a previous blog post.

The package we are going to install is the geradog.gsudo application. We can perform a simple search for this package using winget. To find the correct Windows package we’ll use the winget search command to find the appropriate package.

See this content in the original post

winget search gsudo

Once found, we can then download this package using winget with a simple command. The winget install command will grab the necessary package from GitHub and install it for us. We’ll be installing gsudo with the following command.

See this content in the original post

winget install geradog.gsudo

Once installed we need to make sure that gsudo is correctly added to the Windows Path environment variable.

Adding gsudo To the Windows Path Variable

In an elevated powershell terminal we need to ensure that Windows has the path to gsudo.exe. We can accomplish this as a persistent change with the following series of commands.

First let’s save the current path using the following Get-ItemProperty command.

See this content in the original post

Next let’s append the path to gsudo.exe to our current path.

See this content in the original post

And finally let’s set this new updated path with the Set-ItemProperty command.

See this content in the original post

windows powershell updating path variable process

We can verify that the path was indeed changed to include the path to gsudo in a persistent manner by closing out the powershell console and running the following command.

See this content in the original post

If successful we should see the correct path to gsudo appended to our Windows path variable.

Windows path variable now includes the path to gsudo

Running gsudo

Once we have added the location of gsudo.exe to our Windows PATH variable we can run gsudo from our PowerShell terminal to elevate the our prompt. We can simply run gsudo from the PowerShell terminal

See this content in the original post

gsudo UAC confirmation

When we first run gsudo we will encounter a User-Access-Control (UAC) prompt. In order to elevate our prompt we need to allow this application to make changes.

gsudo elevated prompt window

Once we have successfully run gsudo notice the elevated terminal. We can verify the window is elevated by running gusdo again. If already elevated gsudo will return an already running with elevated permissions error.

gsudo already running with specified user/permission-level error

Great! Our PowerShell terminal is now running as an Administrator. Let’s explore how we can get help with running gsudo.

Getting Help with gsudo Usage

Like most command-line tools we can use the help command to see gsudo usage information as well as common syntax options.

See this content in the original post

gsudo —help command

You can also visit the gsudo documentation page for usage tips and tricks as well as everyday usage. If you run into any issues there is also a common troubleshooting page as well as a known issues page.

Conclusion

In this post we took a look at how to install the gsudo the Windows functional equivalent of sudo. Using gsudo we are able to elevate our terminal to the Administrator account similar to how the sudo command runs commands as the Linux root user.

See this social icon list in the original post

If you have any questions or have issues feel free to message me on social media.