2021 Annual Petri Reader Survey - We want to know what's important to you! 2021 Annual Petri Reader Survey - We want to know what's important to you!
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.

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 (server1.mshome.net) 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 'server1.mshome.net' -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:

BECOME A PETRI MEMBER:

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

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.

Register for the Hybrid Identity Protection (HIP) Europe Conference!

Hybrid Identity Protection (HIP) Europe 2021 - Virtual Conference

Mobile workforces, cloud applications, and digitalization are changing every aspect of the modern enterprise. And with radical transformation come new business risks. Hybrid Identity Protection (HIP) is the premier educational forum for identity-centric practitioners. At the inaugural HIP Europe, join your local IAM experts and Microsoft MVPs to learn all the latest from the Hybrid Identity world.