Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET
Windows Server

How to Run Code Remotely in Visual Studio Code

When you are working on PowerShell scripts, it’s standard practice to use the Windows PowerShell Integrated Scripting Environment (ISE) or other code editor, like the popular Visual Studio (VS) Code from Microsoft. VS Code is certainly not perfect, but Microsoft has stopped developing the PowerShell ISE so if you want the latest experience, VS Code is the way to go.

But what if you are developing code that needs to be tested on a remote device? You could manually copy any code changes to the remote device each time you need to run a test but that is time consuming and a pain because you end up with two copies of the file, one locally and another on the remote device. But what if you could run the code on the remote device without leaving VS Code? With some help from PowerShell Remoting, you can do just that.

Connect to a Remote Device Inside Visual Studio Code

There are a couple of prerequisites that you need to meet before you can use remoting. Both the PC on which VS Code is installed and the remote Windows device must be running Windows PowerShell. That shouldn’t be an issue with any modern version of Windows. PowerShell Remoting uses the Windows Remote Management (WinRM) protocol, which is enabled by default in Windows Server 2012 and later versions. WinRM is installed but disabled by default in client versions of Windows.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

Enable Windows Remote Management

If you want to enable WinRM on a Windows client device, open a command prompt and run the command shown below:

winrm quickconfig

This command starts the WinRM service, configures a listener on the default port, and creates exceptions in the Windows Firewall for the currently logged in user.

Install PowerShell Extension for Visual Studio Code

Before you start, make sure that you have the PowerShell extension for Visual Studio Code installed. Press CTRL+SHIFT+X to open the EXTENSIONS pane. If you don’t see PowerShell listed, type PowerShell in the search box at the top of the EXTENSIONS pane, select the PowerShell extension from Microsoft in the list of results and install it.

Establish a Remote PowerShell Session

If WinRM is listening on the remote device, you can establish a new remote session inside VS Code. All you need to do is run the Enter-PSSession cmdlet with the details of the remote device and the credential you want to use to connect. You can see in the example below that I’m using the remote device’s DNS name ( and connecting to the server using the account ‘administrator’. When the command runs you will be prompted to enter the password for the user in the -Credential parameter.

Enter-PSSession -ComputerName '' -Credential administrator

Once the connection has been made, you’ll see that the prompt in the terminal window changes to reflect the fact that you are now connected to a remote device instead of the local machine. Any code you run will now execute on the remote device instead of locally. For example, if you select some code in a file and then press F8, that code will run on the remote device. In the screenshot below, you can see that I ran Get-Acl ‘c:\accounts’ | Format-List on server1 instead of the PC where VS Code is installed. I selected Get-Acl ‘c:\accounts’ | Format-List in the file and pressed F8.

How to Run Code Remotely in Visual Studio Code (Image Credit: Russell Smith)
How to Run Code Remotely in Visual Studio Code (Image Credit: Russell Smith)

When you are ready to close the remote session, simply type exit in the terminal window and press ENTER.

It is as simple as that and a real productivity booster when you are testing code on remote devices. In a forthcoming article, I’ll show you how establish remote connections inside VS Code using Secure Shell (SSH) and PowerShell Core.


Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: