Run VS Code or the VS Code Terminal as an Administrator

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# Run VS Code or the VS Code Terminal as an Administrator

The easiest way to run the VS Code terminal as an administrator is to:

  1. Search for Visual Studio Code and right-click on the application (or simply right-click on the .exe file).
  2. Select Run as administrator.

run vscode terminal as administrator

  1. When you get prompted, click on Yes to start the application as an administrator.

confirm prompt

# Configuring VS Code and its Terminal to always open as an administrator

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:

  1. Make sure to close all instances of VS Code.

You can also issue a command to close all VS Code instances.

Open CMD and run the following command.

cmd
# Windows taskkill /F /IM code.exe
  1. Search for Visual Studio Code and right-click on the application (or simply right-click on the .exe file).

  2. Click on Open file Location.

right click open file location

  1. Right-click on the Visual Studio Code icon in the folder and select Properties.

right click select properties

  1. Click on the Compatibility tab.

select run this program as administrator

  1. Check the Run this program as an administrator checkbox as shown in the screenshot.

  2. Click on Apply and then OK.

# Opening the terminal with administrative privileges

To open the terminal that has administrator privileges:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type toggle terminal and select View: Toggle Terminal.

vscode open terminal

You can also open the terminal by using a keyboard shortcut:

  • on Windows and Linux: Ctrl + ` (backtick).
  • on macOS: Ctrl + ` (backtick).

Once you open the terminal issue the net session command to verify it runs with administrative privileges.

cmd
net session

verify terminal runs as administrator

If your terminal has administrative permissions, you will see a message similar to the following.

cmd
System error 5 has occurred. Access is denied.

If you are an administrator, you will see a message similar to the following.

cmd
There are no entries in the list.

If you use PowerShell, you might also have to set the execution policy to Unrestricted.

PowerShell
Set-ExecutionPolicy Unrestricted

You can also scope the command to the current process.

PowerShell
Set-ExecutionPolicy Unrestricted -Scope Process

# Use the sudo prefix if you use bash or zsh in VS Code

If you use bash or zsh as your terminal in VS Code, use the sudo prefix to run a command as an administrator.

shell
sudo your_command

For example, sudo npm install -g create-react-app runs the npm install command as an administrator.

shell
sudo npm install -g create-react-app

# Using the runas command

You can also use the runas command to run a command from your terminal as an administrator on Windows.

shell
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.