Adding additional intellisense to VS Code when editing PowerShell scripts

Most people that I talk to about my experiences writing PowerShell scripts these days will know that Visual Studio Code is my absolute preference for editor these days. It’s a great tool with lots of little tricks to help you improve your productivity – and recently I came across another one to help when I work on SharePoint related scripts for SharePointDsc, and this is how I add intellisense for SharePoint PowerShell cmdlets when I’m authoring scripts on my laptop (which doesn’t have SharePoint installed on it).

If you start to rip apart how VS Code provides it’s intellisense for PowerShell you’ll see that it is able to do so through the use of a PowerShell Integrated Terminal – the program keeps this open in the background so that it can make calls to this session to get details to aid it’s intellisense and other PowerShell interactivity.

Now by default if I try typing something like “New-SPSite” in to this PowerShell file you’ll see that the intellisense doesn’t give me anything back to help.

But if we know that VS Code draws its intellisense data from the PowerShell session, we can actually load the SharePoint cmdlets up and have them available. Now if I was running VS Code from a machine that has SharePoint installed it would be easy, I could just type the command to load the snap-in to the terminal and the cmdlets would then be available. However given I don’t have SharePoint installed on my laptop that isn’t an option. I do however know that to make sure the unit tests for SharePointDsc run that we keep non-functional copies of the SharePoint PowerShell cmdlets in what we call “stub” files, which will provide me enough of a structure to get this intellisense without needing SharePoint installed (if you are curious about how we came up with this approach, I have a previous blog post that addresses this topic). So if I want to load those up so I can add the intellisense I just need to use Import-Module to the path of either the 2013 or the 2016 cmdlets.

Import-Module C:\repos\SharePointDsc\Tests\Unit\Stubs\SharePoint\16.0.4456.1000\Microsoft.SharePoint.PowerShell.psm1

In that example I have the source code for SharePointDsc saved at C:\repos\SharePointDsc – so update the path accordingly to where you have the code stored. But once I have run that command you’ll see that the intellisense lights up in full!

This makes it much easier to author scripts without needing to have SharePoint installed, improving my productivity when authoring SharePointDsc. You could use these for any SharePoint script, not just working with SharePointDsc – just make sure you have the code for SharePointDsc around to load the stubs up and then you can use this for any script. I hope that helps improve how you write your scripts guys!

 

 

One Reply to “Adding additional intellisense to VS Code when editing PowerShell scripts”

Leave a Reply