Using Visual Studio Code for PowerShell – Settings

Like many others, I’m giving Microsoft’s free code editor Visual Studio Code a try.

Perhaps a bit late to the party, I know, but I’ve been a big fan of Microsoft’s built in PowerShell ISE editor, especially when combined withย Dr. Tobias Weltner’s ISESteroids.

So what’s this post about?

Nothing special for now, just wanted to share my default Visual Studio Code settings.json file [Ctrl + , ].

This file sets your personal preferences within the editor, making the look and feel just a bit smoother to my liking.
Especially when you’re used to one specific editor, switching to another one might not be as easy as you’d like.

With small setting changes, you can perhaps ease that change..

Some explanation might be handy ๐Ÿ™‚

As you might have noticed, I write mainly about PowerShell items, so my focus on the settings are mainly for the PowerShell experience.

PowerShell first

I’ve configured VSCode to automatically set the language to PowerShell using:

Do note that the language bit is case sensitive and PowerShell, Powershell or any other variation will not work. Lowercase all the way on the language.
This bit saves you from having to define each new file as language PowerShell [Ctrl + K -> M -> ps ]

Of course you also want PowerShell to automatically load on start, making the experience better and quicker to my liking and enabling the default PowerShell application as the integrated shell.

Formatting preferences

Now there are also a few things that work together:

The first 2 are ways that work for me, trimming white spaces at the end of lines and formatting open braces [ { & } ] in a particular way.

The second 2 lines forces VSCode to apply them immediately when you type, instead of having to apply the formatting rules [Shift + Alt + F].

Last but not least

A golden setting for me:

While using VSCode, I noticed this annoying behaviour when just running a small bit of code and executing it through the F8 key…
After executing the code, it would automatically set the focus on the Console section [lower part] instead of the Editor section. If you’d want to do something, you would have to manually select the Editor section again before you can continue.

This little piece of code saves you from that hassle.

Full file


Now I know for some people it’s nothing special, but for me these small settings make a huge difference in my usage experience for Visual Studio Code.

Perhaps you have some other settings you’d like to share, feel free to comment!

Happy scripting! ๐Ÿ™‚


Finding remote registry entries with Remoting disabled…

I had run into a situation where I quickly needed to check registry entries for various machines, but PowerShell remoting had not properly been enabled for all of the machines [and was not allowed to be set up on short notice].

Since I needed to quickly obtain the information for various keys, I turned to Google for advice.

Local and Remote

Firstly I’ll define the registry keys I’d like to have information about:

So normally you can get the Local information using the following code:

If you want to obtain the information Remotely, try the following:

As you can see, I can’t just use the variables provided locally, but since I’m running an up-to-date version of PowerShell [v3+], I can using the Using scope modifier, as described here.

I’ve also quickly created a PSCustomObject to contain all the information I require, so I can output this in a clean way.

But what if Remoting is not enabled?

Have no fear, you can still use PowerShell to access the data you want, just through an alternative means.

In this situation, PowerShell can use the power of .NET to still get what it wants!
Do note that this does require the Remote Registry Service to be running on the machine.

Firstly, because of the way registry keys are interpreted through the .NET class, this means splitting up our $RegKey1 and $RegKey2 value into a $Hive [since both are in HKLM] and $Key values:

Using the Microsoft.Win32.RegistryKey class provided by .NET, you can open up a remote connection to a specific hive [LocalMachine, Users] on a remote machine ย [you can see this method and other methods available to perform various tasks here].

Once you’ve defined the remote registry connection, you can start getting the information you require using the OpenSubKey and GetValue methods.

What does this look like as a final result:

Hopefuly this can help you in a situation where you just NEED to view remote registry entries, but PowerShell Remoting isn’t [fully] configured.

Happy scripting! ๐Ÿ™‚