Last updated: Apr 6, 2024
Reading time·3 min
The easiest way to run the VS Code terminal as an administrator is to:
.exe
file).The previous approach only opens a single instance of VS Code and its terminal with administrative privileges.
If you want to always configure VS Code to open as an admin:
You can also issue a command to close all VS Code instances.
Open CMD and run the following command.
# Windows taskkill /F /IM code.exe
Search for Visual Studio Code and right-click on the application (or
simply right-click on the .exe
file).
Click on Open file Location.
Check the Run this program as an administrator checkbox as shown in the screenshot.
Click on Apply and then OK.
To open the terminal that has administrator privileges:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.You can also open the terminal by using a keyboard shortcut:
Ctrl
+ ` (backtick).Ctrl
+ ` (backtick).Once you open the terminal issue the net session
command to verify it runs
with administrative privileges.
net session
If your terminal has administrative permissions, you will see a message similar to the following.
System error 5 has occurred. Access is denied.
If you are an administrator, you will see a message similar to the following.
There are no entries in the list.
If you use PowerShell, you might also have to set the execution policy to
Unrestricted
.
Set-ExecutionPolicy Unrestricted
You can also scope the command to the current process.
Set-ExecutionPolicy Unrestricted -Scope Process
sudo
prefix if you use bash
or zsh
in VS CodeIf you use bash
or zsh
as your terminal in VS Code, use the sudo
prefix to
run a command as an administrator.
sudo your_command
For example, sudo npm install -g create-react-app
runs the npm install
command as an administrator.
sudo npm install -g create-react-app
runas
commandYou can also use the runas command to run a command from your terminal as an administrator on Windows.
runas "/user:Administrator" "notepad my_file.txt"
Make sure to enclose commands that contain spaces in double quotes.
When you issue the command, you will be prompted for the administrator account password.
I've also written an article on how to add a run button in VS Code.
You can learn more about the related topics by checking out the following tutorials: