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.

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

1
2
3
"files.defaultLanguage": "powershell",
"terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
"powershell.startAutomatically": true

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.

Now there are also a few things that work together:

1
2
3
4
"files.trimTrailingWhitespace": true,
"powershell.codeFormatting.openBraceOnSameLine": true,
"editor.formatOnSave": true,
"editor.formatOnType": true

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

A golden setting for me:

1
"powershell.integratedConsole.focusConsoleOnExecute": false

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
    "files.autoSave": "off",
    "editor.rulers": [
        120
    ],
    "files.defaultLanguage": "powershell",
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
    "powershell.startAutomatically": true,
    "files.trimTrailingWhitespace": true,
    "powershell.codeFormatting.openBraceOnSameLine": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "powershell.integratedConsole.focusConsoleOnExecute": false
}

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! 🙂