Yesterday David Wilson announced version 1.4.0 of the PowerShell extension for VSCode
He also pointed out that there have been over 1 million installs of the extension.
If you want to install the PowerShell extension you can hit F1 in VSCode and type
ext install PowerShell
or CTRL + SHIFT + X to open the extensions side bar and search for PowerShell and click the green install button.
There are a few enhancements in this release which you can read about here but I noticed the New File API and Out-CurrentFile command that were contributed by Doug Finke.
If your focus is in the editor in VSCode, you can simply CTRL + N and create a new file. You can alter the language that the file uses with CTRL + K, M (that’s CTRL and K and then M not CTRL and K and M!) or set the default new file language as I described here.
If you are using VSCode as your daily PowerShell command line though, you would have to alter your focus from the terminal panel into the editor to do this. Now though you have
$psEditor.Workspace.NewFile()
which enables you to create a new file from the terminal
The Out-CurrentFile command sends the output of a command through Out-String to a new file. This makes it much easier to keep the results of some commands. You don’t have to pipe them to clip or highlight and CTRL + C to copy them and then open a file and paste them. Of course you can use Out-File and then open the file but this is another way.
Lets see how it works. In this example, I want to export the T-SQL for creating the logins on an instance and add some comments to the code before saving it somewhere safely. I am going to use the Export-SQLLogin command from the dbatools module. MVP Cláudio Silva has written a great post on that command today.
First create a new file
$psEditor.Workspace.NewFile()
and then
Export-SqlLogin -SqlInstance . | Out-CurrentFile
In the gif above
- I create a new file,
- Export the logins to it
- change the language of the file to T-SQL
- remove the string quotes and
- add some comments.
All without leaving VSCode, just another reason that my productivity is increased using VSCode!
Unfortunately, it doesn’t work so well with Pester. (Of course I was going to try Pester!). This makes sense though, as Pester uses Write-Host to display the test results so nothing is going to the output stream so
Invoke-Pester ‘.\SQL Grillen\Demo Number 3.Tests.ps1’ | Out-CurrentFile
returns this
and if you use the -PassThru parameter then you get the $Tests object as a string so
Invoke-Pester ‘.\SQL Grillen\Demo Number 3.Tests.ps1’-Show Summary -PassThru | Out-CurrentFile
shows
Which isn’t what I would need but I have other ways of working with Pester output.