r/PowerShell • • Apr 15 '21

Question What do you guys do to setup Visual Studio Code for PowerShell?

Hi everyone! I've been fooling around with Visual Studio Code and the PowerShell extension and wanted to see how everyone else configured theirs. Some drawbacks I've noticed is that Intellisense won't pickup some imported module arguments (looking at you MSGraph) within the scripting pane. What's your experience? Have any tips on making it a full replacement for PowerShell ISE? Thanks!

Edit: Here's an example of my settings.json:

"editor.defaultFormatter": "ms-vscode.powershell",
"editor.tabCompletion": "on",
"files.autoSave": "off",
"workbench.iconTheme": "vs-seti",
"files.defaultLanguage": "powershell",
"powershell.integratedConsole.focusConsoleOnExecute": false,
"powershell.integratedConsole.showOnStartup": false,
"editor.renderWhitespace": "all",
"editor.renderControlCharacters": true,
"files.trimTrailingWhitespace": true,
"files.encoding": "utf8bom",
"files.autoGuessEncoding": true,
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"

I set the integrated shell this way since I want to respond to modern authentication requests.

Can you guys think of anything else I should add?

136 Upvotes

101 comments sorted by

8

u/ZomboBrain Apr 15 '21

2

u/--butt-hurt Apr 15 '21

This is cool! I'll be reading through this today. Thanks!

1

u/rottenrob325 Apr 16 '21

Hoping this article shows how to run selection in terminal. That's the best part of ISE, imo

2

u/--butt-hurt Apr 16 '21

Oh, I found that functionality already existed. Run selection (F8) option in upper right hand corner.

3

u/cottonycloud Apr 16 '21

I use Better Align, PowerShell extension, and TabOut.

As for configuration:

"powershell.codeFormatting.preset": "Allman"
"powershell.codeFormatting.useCorrectCasing": true,
"powershell.codeFormatting.autoCorrectAliases": true,
"powershell.sideBar.CommandExplorerVisibility": false,

2

u/--butt-hurt Apr 16 '21

Thanks for sharing!

3

u/mooscimol Apr 17 '21

Wow, thank you for this. I didn't know about formatting preset. I had it defaulted to Stroustrup, although I always manually corrected else, catch, etc. to the same line as closing bracket, so I could run selection in terminal.

It turned out that OTSB style is doing exactly what I was doing manually for years, LOL.

BTW. assigning shortcut for Terminal: Runs Selected Text In Active Terminal is a must IMO for effectively using VSCode (not only for PowerShell) :) - I'm using F8 of course, like in ISE.

2

u/cottonycloud Apr 17 '21

I believe the keybinding already default to F8. I have not had to configure that before.

1

u/mooscimol Apr 17 '21

Ah, yes PowerShell extension sets it right now by default PowerShell: Run Selection. It wasn't the case a few years ago I believe. The thing is, that it works only for PowerShell, and if you're editing any other files like .sh it won't do anything, so I prefer above assignment.

6

u/artemis_from_space Apr 15 '21

Yeah intellisense is really broken for me also. I enjoy letting it format my file when I save.

Also sync of settings is nice as I have multiple machines I work on.

Start using git or similar to keep revisions of code.

Set language to powershell as default.

3

u/hellphish Apr 15 '21

Intellisense is hit and miss for me on the release version, but the powershell preview extension has been working well for me.

1

u/--butt-hurt Apr 16 '21

Dude! I agree, the preview extension responds MUCH better with Intellisense.

2

u/ZomboBrain Apr 15 '21

I enjoy letting it format my file when I save.

How do you do that? Didn't find that feature, yet.

3

u/mystikphish Apr 15 '21

Search in settings for "format on save" should get it. I personally don't like it because it can be super slow if you are working on DSC stuff for dinner reason. I just use the short-cut shift-alt-f after any major editing.

1

u/replicaJunction Apr 16 '21

DSC stuff for dinner reason

Dinner / supper choice?

3

u/mystikphish Apr 16 '21

I deploy with Chef, so...

2

u/BlackV Apr 15 '21

the intelli sence is quite bad for powershell.
its works 80 ish percent of the time

or decides it wants to insert a whole snippet instead of the parameter I do want

1

u/cottonycloud Apr 16 '21

I wish I could set it to format on save, but the formatter is not satisfactory for me in many ways. For example, setting indent after the first pipeline is not doing so correctly with the latest update. I would also like to have a way to exclude certain blocks from being formatted.

1

u/efiefofum Apr 16 '21

On large files my script gets completely mangled. There's an open issue on github from some time ago but no one has responded to requests for information in months.

1

u/AlexHimself Apr 16 '21

"letting it format my file"? What does that mean?

And why is the intellisense so busted?!

31

u/thanatos8877 Apr 15 '21

I started using VSCode exclusively about 5 years ago after reading somewhere that ISE development was not going to be moving forward. I have always installed the Powershell and vscode-icons extensions.

There are also a couple of settings that I have always used in my VSCode settings to make my PowerShell-ing easier for me.

"workbench.colorTheme": "PowerShell ISE",
"files.defaultLanguage": "powershell"

The theme makes VSCode look more like ISE and helped me ease into the change. Now, I keep it because I prefer the look. Almost everything that I do in VSCode is PowerShell, so I set the default language to be PowerShell.

27

u/[deleted] Apr 15 '21

Man I’m exactly the opposite on the color theme thing. powershell ise was always too bright and in my face. I had a wholly custom color theme that I used to use back in the day, but since I swapped to VSCode, I just use the Noctis theme from the extension store.

5

u/_benp_ Apr 15 '21

Custom color palettes all the way. Vim blackboard is a great one for a starting point.

3

u/replicaJunction Apr 16 '21

The Noctis extension comes with a bunch of different themes. Out of curiosity, which variant do you prefer?

61

u/KyleKowalski Apr 15 '21

Open a .ps1 in VS Code. It prompts to install powershell extension. Agree. Begin coding. :)

2

u/elevul Apr 16 '21

Same! Since I'm constantly on different machines I gave up customizing.

9

u/DevelopingStorm Apr 16 '21

Look into the new (at least to me) setting sync feature. You can save your setting to a MS account or a GitHub account and sync all your settings across multiple PC’s

It actually makes me want to code at home after spending some time setting everything up at work

2

u/OIT_Ray Apr 16 '21

Sync never works properly for me. I bounce between two machines and each always says there's a conflict.

3

u/M374llic4 Apr 17 '21

I don't use the built-in one, I use an extension called Settings Sync.

Between my Linux desktop and laptop, Windows VMs on Unraid, work laptop and other work-related VM's, I have not had any issues between them. You can also designate certain settings using comments as "per machine", "Linux/Windows only", etc.

3

u/OIT_Ray Apr 17 '21

That's much better. Thanks for the link!

2

u/M374llic4 Apr 17 '21

Sure thing, no worries. 👍

1

u/elevul Apr 16 '21

But you have to login every time

0

u/[deleted] Apr 16 '21

[deleted]

0

u/elevul Apr 16 '21

Yeah, but I mean on each new PC you work on

1

u/M374llic4 Apr 17 '21

If thats too much for you, you could just have a VSCode Portable install on a flash drive and just always use that.

1

u/elevul Apr 17 '21

Uh, that's genius! I can have it in azure storage and pull it down via invoke-webrequest!

1

u/nohwnd Apr 16 '21

You can have vs installation in your profile IIRC, In case you’re at least in the same domain. Or your dropbox or something like that.

0

u/JiveWithIt Apr 16 '21

This then add monokai theme

17

u/dasookwat Apr 15 '21

personally, i also add: bracket colorizer, and depending on where i install it (work job environment) gitlens

2

u/TheGooOnTheFloor Apr 16 '21

How did I not know this? Sweet!

13

u/[deleted] Apr 15 '21

[removed] — view removed comment

6

u/Carpathium Apr 15 '21

Indent colorized was suggested when I reinstalled VSCode the other day. Thought it was gimmicky cause of rainbow brackets and good formatting but installed it anyway.... holy cow, idk how I went this long without it. It’s sooo easy track stuff lol

6

u/[deleted] Apr 15 '21

Indent colorizer

I can't seem to find that, should it be "indent-rainbow"?

2

u/[deleted] Apr 15 '21

[removed] — view removed comment

1

u/[deleted] Apr 15 '21

Nice one, thanks. Adding it now.

1

u/idontknowwhattouse33 Apr 16 '21

He got us both. Was going off the top of my head in a rush, haha.

3

u/Thotaz Apr 15 '21
  • Change various keybindings (Most importantly shift+alt + arrow keys to sort of emulate the column selection found in pretty much any other editor).

  • Change a ton of settings to disable all sorts of fancy help features that I find distracting.

  • Delete built-in snippets and replace them with my own (no way to disable built-in snippets and they are recreated anytime the extension is updated).

  • Replace the grammar file for Powershell with a much more accurate one along with my own custom theme (grammar file needs to be replaced after every VS code update).

It's a bit annoying to have to change so much, but with all of those changes the PS experience can almost be considered good.

3

u/neztach Apr 16 '21

You wouldn’t happen to have a write-up explaining your changes would you?

4

u/Thotaz Apr 16 '21

I don't, but I can make a quick one here. This is my settings file:

{
    "editor.autoClosingBrackets": "never",
    "editor.autoClosingQuotes": "never",
    "editor.autoIndent": "keep",
    "editor.fontFamily": "Lucida Console",
    "editor.fontSize": 12,
    "editor.lineHeight": 13,
    "editor.matchBrackets": "near",
    "editor.minimap.enabled": false,
    "editor.mouseWheelScrollSensitivity": 0.65,
    "editor.mouseWheelZoom": true,
    "editor.occurrencesHighlight": false,
    "editor.parameterHints.enabled": false,
    "editor.quickSuggestions": {
        "comments": false,
        "other": false,
        "strings": false
    },
    "editor.renderLineHighlight": "none",
    "editor.selectionHighlight": true,
    "editor.showFoldingControls": "always",
    "editor.snippetSuggestions": "none",
    "editor.suggest.filterGraceful": false,
    "editor.suggestSelection": "first",
    "editor.tabCompletion": "on",
    "editor.wordBasedSuggestions": false,
    "files.defaultLanguage": "powershell",
    "powershell.buttons.showPanelMovementButtons": true,
    "powershell.codeFormatting.openBraceOnSameLine": false,
    "powershell.codeFormatting.useCorrectCasing": true,
    "powershell.integratedConsole.focusConsoleOnExecute": false,
    "powershell.integratedConsole.forceClearScrollbackBuffer": true,
    "powershell.powerShellDefaultVersion": "Windows PowerShell (x64)",
    "terminal.integrated.cursorBlinking": true,
    "terminal.integrated.cursorStyle": "line",
    "terminal.integrated.fontFamily": "Lucida Console",
    "terminal.integrated.fontSize": 12,
    "terminal.integrated.mouseWheelScrollSensitivity": 0.3,
    "window.zoomLevel": 0,
    "workbench.sideBar.location": "right",
    "workbench.startupEditor": "newUntitledFile"
}

It's smaller than what I remember. I disable auto closing stuff because it's never been an issue for me and I don't want to start relying on it because I can't always expect to have it (my console for example won't have it).
I increase the information density by lowering the fontsize and reducing the spacing between each line because I have a good set of eyes that don't need the ridiculously large size used by default.
I disable the minimap because I don't think the preview gives me any value.
I disable most of the highlighting except the one I get from manually selecting a word because I generally don't care where my variables are referenced unless I'm actively looking for it.
I disable all of VS code's automatic intellisense triggers because the PS extension has its own set of triggers that aren't as aggressive.
I remove inline suggestions from the intellisense pop up so it's only suggestions provided from PS itself that pops up (I have a separate hotkey for snippets).
I don't remember how much of an impact filterGraceful has on the intellisense, but even with it disabled I still think VS code is a bit too eager to show irrelevant suggestions.
Tab completion is nice because it lets you correct the casing of a copy+pasted command or variable without having to invoke the intellisense menu and selecting the only item. I rarely used it to cycle through options.

This is the grammar I'm using: https://github.com/PowerShell/EditorSyntax/pull/156 except I'm using the slightly modified version that is mentioned in the comments to properly highlight commands without the approved verb-noun syntax.

2

u/--butt-hurt Apr 16 '21

Thanks for sharing and explaining your settings! There'll be a few additions to mine after reading this :)

11

u/DaNPrS Apr 15 '21 edited Apr 15 '21
  • Powershell Extension, of course!

  • Better Powershell Syntax Highlighting

If you're not using Git, you probably should. And with that, Github and GitLens extensions are nice. CodeAcademy has a nice tutorial on how to use Git, if you need it.

There's also Azure extensions if you use Azure.

There's extensions to help with DSC, if you're into that.

There's Pester extensions, paid extensions, Docker, grammar, comments and notes extensions... and hundreds of themes.

VSC is like a High-End Exotic Sports Car, compared to ISE. Get used to it, it's so much better. Intelisense will work if you have the Powershell extension installed. If you have issues with a Module, try to import it and Run the Debug. It should work after that.

4

u/[deleted] Apr 15 '21 edited Apr 15 '21

What does the git extension give you extra? I just install git on my computer and use vscode built in commit and push functionality

2

u/lerun Apr 16 '21

If you want to bedazzle the terminal some more look into posh-git and oh-my-posh

2

u/DaNPrS Apr 15 '21

Git is a change tracker.

It tracks who made changes to a file, when, and what changes. You can see an example here. The red lines were removed, and the green ones added.

Furthermore, if you have several people contributing to the same file, you can identify who did what, and when.

Since I do most of the scripting at my place, I keep a Git Repo for myself to keep track of my stuff.

There are Git providers/vendors, like GitHub and GitLab that wrap a nice GUI, interface and Auth on top of all that.

Take 30mins to go through this course and you'll get a better understanding of it's capabilities and how you can use it to organize your scripts. https://www.codecademy.com/learn/learn-git

2

u/[deleted] Apr 15 '21

Nah I know what git is. My repos live in Azure. I meant I don't know what the git extension in vscode would do since git is typically installed system wide.

1

u/DaNPrS Apr 15 '21

It gives you many of the same features of GitHub, but on VSC. You can login login to GitHub, Azure Pipelines and others, right from VSC, and commit from it. Also see repo changes and what not.

https://code.visualstudio.com/docs/editor/versioncontrol

2

u/[deleted] Apr 15 '21

That's always been built in VSCode for me. I have never installed anything extra.

1

u/blooping_blooper Apr 16 '21

I think maybe it installs/enables the git extension automatically if you open a git-controlled folder?

-6

u/jantari Apr 15 '21

Why don't you just read its features instead of asking

8

u/[deleted] Apr 15 '21

Because it might have some benefit to this person I can't glean.

4

u/AlexHimself Apr 16 '21

My intellisense constantly stops working! Any idea why?

I'm similar to OP where I only use for powershell and I've got everything updated.

1

u/overlydelicioustea Apr 16 '21

standard behavior for me aswell. reloading code and its back again.

7

u/[deleted] Apr 15 '21

I mean I had to install pscore. But it's much much better.

5

u/boffhead Apr 16 '21

Use the addon "Shell launcher" to swap between PS5.1 and Ps7 with a keybind: control-shift-t in the below example.

Also mandatory F8 to run selected code.

learning Control + / for commenting is also really useful.

keybindings.json for vscode:

// Place your key bindings in this file to override the defaultsauto[]
[
    {
        "key": "f8",
        "command": "workbench.action.terminal.runSelectedText",
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+shift+t",
        "command": "shellLauncher.launch"
    }
]

2

u/mooscimol Apr 17 '21

This. F8 shortcut, and shell launcher extension are always the first things I configure on new VSCode setup.

8

u/32178932123 Apr 15 '21

Personally I really don't like the ISE but that's because I joined the Powershell party too late. If it had a dark mode I may have been ok with it but I pretty much went straight for VSCode.

All you really need to do is download VSCode, open a .Ps1 file in it and accept the prompt to install the powershell extension.

But these are the extensions I also use:

  • Settings Sync by Shan Khan - Just means whenever I install VSCode again I can enter my git code and it'll download my config, hot keys, extensions etc. I don't just use Vscode for Powershell

  • Todo Tree - I like this one because if you start a comment with #TODO: then it'll add it to a list where you can easily see the things you need to go back to

  • Code Runner - Takes a bit of config for things like Python but basically this lets you use the shortcut Ctrl+Alt+N to execute your code. I found it quicker than waiting for a Powershell Terminal or debugger to spin up when I didn't need it. You might have to configure it in Extensions Settings to make it use the right version of Powershell though if you've got Core installed. I also go in settings and check "Run in Terminal" so I can respond to any prompts for input in the terminal.

  • Indent Rainbow - I can't explain very clearly. A picture says a thousand words so click on the link and look at the picture!

Personally my experience has been good but I don't do MSGraph or anything like that. My main gripe is when I type "[" and then auto complete I end up with two square brackets at the beginning but it's probably something I'm doing.

2

u/replicaJunction Apr 16 '21

Have you tried the official settings sync now that it's out of beta? I used to use a custom Github script and symlinks to synchronize my settings myself, but I've found that the official settings sync works for everything I need it to do now.

1

u/32178932123 Apr 16 '21

You know, I just stumbled across it this week but when I clicked it, I get a warning saying it's in Preview so I didn't crack on. I'll give it a go!

1

u/BlackV Apr 15 '21

that square brackets one shits me a lot

1

u/32178932123 Apr 16 '21

Glad it's not just me! I thought I was doing something wrong.

1

u/BlackV Apr 16 '21

Yeah especially how often I type [pscustomobject]

Even if you select the auto complete it still puts it there

1

u/nohwnd Apr 16 '21

Need to try todo tree.

4

u/replicaJunction Apr 16 '21

There are some absolutely amazing extensions for VS Code, but be careful with performance - extension bloat is real, and it will affect editor performance.

You can actually choose to enable extensions on a folder-by-folder basis if you regularly open folders instead of single files. Disable the extensions you don't want globally, then open a folder and in the extensions menu choose "Enable (Workspace)" instead. That way, you can install all the GitHub extensions you want (for example), then disable them unless you're working on a project that has a GitHub repo.

Also, get familiar with the Ctrl+Shift+P shortcut to open the command palette. This has all of the menu options and a lot more commands that aren't in menus, and you can activate it without taking your fingers off of the keyboard.

4

u/da_chicken Apr 16 '21

I just use it as-is with the extension.

Honestly, I spend more time copying code to an external terminal window. The code completion in vs code breaks constantly if you do anything beyond a beginner script, and God help you if you edit a line instead of write perfect code from the get-go. It won't understand the difference between a variable, a statement, or a command. Meanwhile the PSReadLine completion at the actual command line is consistently rock solid. I'm not waiting 3 minutes for VS code to figure out what the parameters are for Get-CimInstance. It's faster to copy the code to the terminal window and back. Nevermind that it just wants to complain about the XML fragment in a document that isn't even active.

And don't get me started on the terminal pane randomly deciding it wants to mangle your code or forget that a library was loaded or try to take control of the shell environment because it thinks it's in charge of doing nothing but calling the C# compiler.

I love vs code, but holy shit is it godawful at the things that should be it's strengths.

3

u/Federal_Ad2455 Apr 16 '21

I have exactly same problem as you :-( intelli sense is complete disaster. Which makes me angry because otherwise vsc is great IDE. I even tried to return back to ISE but it just have intelli sense, nothing more 😁 so stick with VSC

2

u/mooscimol Apr 17 '21

Interesting, because I have hundreds of scripts, that are hundreds of lines and I haven't experienced what you're writing about. I write Get-Ciminstance and parameters show in milliseconds once I type the following -.

Another example, I write add-app (don't remember the command exactly), PSReadline will show me only commands that starts with add-app, but in editor I can see all commands with add and app phrases so e.g. Add-AppPackage and Add-AzApplicationGatewayCustomError or even Add-AzApiManagementUserToGroup down there.

Personally I hardly switch to terminal, I prefer writing everything in editor (except of some ad-hoc commands) and then run them with F8 shortcut.

3

u/yutsoku Apr 15 '21

Create a new file in vs code then edit ir

3

u/jimb2 Apr 16 '21

Disable unwanted languages:

Go to Extensions

Type @builtin

Disable all the language extensions you don't expect to use. Click on each one then disable.

This removes them from the Save As dialog.

3

u/smaight Apr 16 '21 edited Apr 23 '21

There is a video out by Justin Grote which is great. Taken a lot of things to make it easy for PowerShell scripting. He has published a collection of extensions which are great, if not indispensable to help with code writing.

My personal favourites are Todo tree, Bracket pair coloriser, colour-indent and markdownlint. My settings have deviated a bit from what he is lining out in the video, but that is to be expected. Viscose is like a hundred Swiss army knives rolled into one, wouldn't miss it on any machine I am working with :)

2

u/[deleted] Apr 15 '21

Install powershell extension. In general I install git system wide and set before to default dark theme. I used to love abyss but with natural light working from home the dark is more visible than abyss.

2

u/markdmac Apr 16 '21

I installed PowerShell Pro tools so I can make GUI applications from PowerShell and can compile to EXE files.

2

u/SpacezCowboy Apr 16 '21

A Bit much, but I like my extensions.

  • Bracket Pair Colorizer

  • Code Spell Checker

  • Error Lens

  • Hungry Delete

  • Linux Themes for Vscode

  • Markdown Preview

  • Powershell

  • Project Manager

  • Tab Out

  • Todo Tree

  • vscode-icons

2

u/onlychandu Apr 16 '21

I use Spell check extension that supports Camel case variable names. It saved me from silly typos.

2

u/iceph03nix Apr 16 '21

Powershell add in and bracket colorizer are two that jump straight to mind.

The other add-ons that come to mind as sort of related are for reading data files like rainbow csv and Jason/xml add ins.

2

u/OhmegaWolf Apr 16 '21

If you can afford it/have a generous boss, get Powershell Studio it's fantastic

1

u/pppppppphelp Apr 16 '21

If you use a Macbook, can you install VSCode and then the powershell module to interact with your environment?

1

u/almcchesney Apr 16 '21

Something to note the intellisense that shows up is integrated with the "powershell integrated debug terminal". If when it pops up you dot source a function or import module it makes the intelligence available while authoring.

1

u/Stroniax Apr 16 '21

Curious if anyone's aware of an extension that lets me see help comments when I mouse over a command or have it selected in Intellisense like when you do so with a type or class in C# within Visual Studio?

1

u/su6540 Apr 16 '21

1

u/Federal_Ad2455 Apr 17 '21

Can you be more specific? It's quite long video

1

u/su6540 Apr 17 '21

I was able to follow at 1.25 speed 🙂 after viewing I regretted not watching earlier

1

u/Federal_Ad2455 Apr 17 '21

I checked it now and agree that there is few good tips I will use myself

1

u/philmph Apr 15 '21

I have built a custom config over time which helps me with coding very much. Some are very good extensions to boost your visual experience. Not Additionally I wrote my own set of PowerShell Snippets.

1

u/idontknowwhattouse33 Apr 15 '21

Indent highlighter

1

u/[deleted] Apr 15 '21

I keep it mostly stock aside from adding extensions.

1

u/[deleted] Apr 16 '21

[removed] — view removed comment