Skip to content

Instantly share code, notes, and snippets.

@jonshipman
Created December 9, 2022 19:43
Show Gist options
  • Save jonshipman/fe849f7ce841a84be48ee168606771d6 to your computer and use it in GitHub Desktop.
Save jonshipman/fe849f7ce841a84be48ee168606771d6 to your computer and use it in GitHub Desktop.
sudo implementation for powershell
# Place this function in you $Env:Profile
# Has worked with most of what I've thrown at it.
# E.g. `sudo cmd.exe /C mklink /J C:\Test C:\Windows`
function Sudo {
[string] $Location = (Get-Location)
$NewArgs = @()
For ( $i = 0; $i -Lt $args.Length; $i++) {
$NewItem = "$($args[$i])".Replace(" ","`` ").Replace("'","``'").Replace('"','``"')
If ($NewItem -Match " ") {
$NewItem = '"' + $NewItem + '"'
}
$NewArgs = $NewArgs + $NewItem
}
pwsh -Command "Start-Process pwsh -Verb RunAs -WorkingDirectory '$Location' -ArgumentList '-Command', '$NewArgs'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment