VSCode – PowerShell extension 1.4.0 new command Out-CurrentFile

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

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.

01 -new file.gif

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

which enables you to create a new file from the terminal

02- Another new file.gif

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

and then

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

returns this

04 - pester.PNG

and if you use the -PassThru parameter then you get the $Tests object as a string so

shows

05 - pester object.PNG

Which isn’t what I would need but I have other ways of working with Pester output.

4 thoughts on “VSCode – PowerShell extension 1.4.0 new command Out-CurrentFile

  1. You can get your Pester test output (or any Write-Host output) into Out-CurrentFile by simply redirecting all output to the stdout stream. For example:
    Invoke-Pester -Script .\Pester.Commands.Cmdlets.Archive.Tests.ps1 *>&1 | Out-CurrentFile
    PowerShell 5 was released with this support, with Write-Host output being sent to the Information stream (id:6), and that is what allows this to work. If you only want to capture the information stream output but not the errors, simply replace the * with 6 in the command above and you’ll be good to go.

    • Another way to do the same:
      Invoke-Pester -Script .\Pester.Commands.Cmdlets.Archive.Tests.ps1 -InformationVariable info
      $info | Out-CurrentFile
      That gives you on-screen Pester results plus the ability to capture the output in a file when you want it. The output in the file is a little different because Pester uses Write-Host to control the output it sends, but capturing this and then redirecting it to a file does not allow the output to be captured the same way (each Write-Host call results in a new line in the InformationVariable data that is captured, even if Write-Host was invoked with -NoNewline).

  2. Pingback: PowerShell Module for the SQL Server Diagnostics API – 1st Command Get-SQLDiagRecommendations | SQL DBA with A Beard

Please feel free to comment on this post. All comments are moderated first before appearing on the site