Whenever I need to run a powershell script it complains of security, if I add powershell.exe -nologo -executionpolicy bypass -File .\install.ps1
I still get permission denied unauthorizedAccessException. I just want to run this install script, what is the sudo equivalent to type on the powershell on windows?
Edit:
It's 2024 now and we are getting actual sudo for Windows 11
https://devblogs.microsoft.com/commandline/introducing-sudo-for-windows/
See the repository for additional details
Note: If you're looking to add general-purpose, prepackaged sudo
-like functionality to PowerShell, consider the
Enter-AdminPSSession
(psa
) function from this Gist, discussed in the bottom section of this answer.
If you are running from PowerShell already, then use Start-Process -Verb RunAs
as follows:
Start-Process -Verb RunAs powershell.exe -Args "-executionpolicy bypass -command Set-Location \`"$PWD\`"; .\install.ps1"
Note:
$env:windir\System32
, a Set-Location
call that switches to the caller's working directory ($PWD
) is prepended.
pwsh.exe
) this is no longer necessary, because the caller's current location is inherited.Set-Location
necessitates the use of -Command
instead of -File
.
-Command
can change the way arguments passed to your script are interpreted (there are none in your case), because they are interpreted the same way they would be if you passed the arguments from within PowerShell, whereas -File
treats them as literals.If you're calling from outside of PowerShell, typically from cmd.exe
/ a batch file, you need to wrap the above in an outer call to powershell.exe
, which complicates things in terms of quoting, unfortunately:
powershell.exe -command "Start-Process -Verb RunAs powershell.exe -Args '-executionpolicy bypass -command', \"Set-Location `\"$PWD`\"; .\install.ps1\""
Interactively, of course, you can:
Right-click the PowerShell shortcut (in your taskbar or Start Menu, or on your Desktop), select Run as Administrator
to open a PowerShell window that runs with admin privileges, and run .\install.ps1
from there.
Alternatively, from an existing PowerShell window, you can open a run-as-admin window with Start-Process -Verb RunAs powershell.exe
, as in AdminOfThings' answer.