In Unix/Linux systems, sudo !!
is an incredibly useful shortcut that:
- Re-executes the previous command with elevated privileges
- Saves typing when you forget to prepend
sudo
- Works across most *nix shells including bash, zsh, etc.
While PowerShell doesn't have an exact single-command equivalent, we can achieve similar functionality through several approaches:
Method 1: Start-Process with -Verb RunAs
# Store last command in variable
$lastCommand = (Get-History -Count 1).CommandLine
# Execute with elevation
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit", "-Command", $lastCommand
Method 2: Custom Function in Profile
Add this to your $PROFILE for permanent access:
function sudo-last {
$cmd = (Get-History -Count 1).CommandLine
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit", "-Command", $cmd
}
Method 3: Using PSReadLine (PowerShell 5.1+)
# After installing PSReadLine module
function Invoke-SudoLast {
$lastCmd = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()[-1].CommandLine
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit", "-Command", $lastCmd
}
Set-Alias 'sudo!!' Invoke-SudoLast
These techniques work well for most commands, but be aware of:
- Multi-line commands may need special handling
- Commands with double quotes require escaping
- Some interactive commands won't work properly
If you prefer to elevate the entire session instead of spawning a new window:
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit", "-Command", "cd '$pwd'"
Exit
}
Remember that:
- Always verify the command you're elevating
- Consider adding a confirmation prompt to custom functions
- Audit your $PROFILE if used in shared environments
For Unix/Linux users, sudo !!
is an essential time-saver that re-executes the previous command with elevated privileges. PowerShell users often miss this convenient shortcut when working in Windows environments.
PowerShell doesn't have a direct single-command equivalent, but we can achieve similar functionality through these approaches:
# Method 1: Using Invoke-Expression
function sudo-last {
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit","-Command","$(Get-History -Count 1).CommandLine"
}
For a more complete solution that handles edge cases:
function Invoke-Admin {
param(
[switch]$LastCommand
)
if ($LastCommand) {
$cmd = (Get-History -Count 1).CommandLine
if (-not $cmd) {
Write-Warning "No command history found"
return
}
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit","-Command","$cmd"
}
else {
Start-Process pwsh -Verb RunAs
}
}
Set-Alias -Name sudo -Value Invoke-Admin
# Run the last command as admin
sudo -LastCommand
# Alternative short alias usage
sal !! 'sudo -LastCommand'
!! # Now works like sudo !!
1. UAC prompts will still appear (as with sudo)
2. The new window behavior differs from Unix's in-place escalation
3. Complex commands with pipes or redirections may need special handling
For those preferring a single-line solution without functions:
Start-Process pwsh -Verb RunAs -ArgumentList "-NoExit","-Command","$(Get-History -Count 1).CommandLine"