Saturday, June 08, 2019

Using PSEdit from the PowerShell Console - There's a script for that!

The PowerShell ISE has a neat built-in function, PSEDIT. It brings one or more files up into exit windows. I used to use this a lot (in the days before I discovered VS Code!). The PSEDIT function used the $PSISE object to add the files into edit windows. But this does not work from withing the PowerShell console.

But as ever, there's a script for that!  Just add the following function definition into your PowerShell 5.x console:

Function PSEDIT {
[CmdletBInding()]
param([Parameter(Mandatory=$true)]$filenames)
foreach ($filename in $filenames) {
dir $filename | 
  where {!$_.PSIsContainer} |
     %{
        start-process $Pshome\powershell_Ise.exe $filename
      }
   }
}

Once you restart PowerShell, you now have that command avaialble in the console.

Of course, in PowerShell 7, this function does not work since there is no PowerShell ISE V7. In PowerShell 7, you should be using VS Code. Once you install VS Code, typing Code $FileName just works as you would expect - and either start a new instance of VS Code with that file or add the file to an open instance. Very civilised - and another reason I love VS Code.

No comments: