Have you used PSEdit to open a file in a new tab in your PowerShell ISE session?
PSEdit is a built-in function that runs only on PowerShell ISE. It’s simple and easy to use. Enter in a filename and *poof*, it opens in a new tab of the ISE. What a great idea! But wait! There’s more! I’m going to share a function that you can use to put PSEdit on your PowerShell console, (not on the ISE), and since you’re not going to be editing in the console, it opens your files in the ISE.
Here’s an example of how PSEdit is used:
#Declare the file you want to edit into a variable (optional) $FilePath = "C:\Scripts\MyProfile.ps1" #Run the file PSEdit $FilePath
This is all because of the function “PSEdit” that leverages the $PSIse system variable. Here’s the code for the function.
param([Parameter(Mandatory=$true)]$filenames) foreach ($filename in $filenames) { dir $filename | where {!$_.PSIsContainer} | %{ $psISE.CurrentPowerShellTab.Files.Add($_.FullName) > $null } }
The only problem is that it’s only available in ISE. Of course, you don’t really have an editor in a PowerShell console session, and I’m almost always in ISE when I’m wanting to edit… But it might be nice to include that function in a PowerShell console.
If you’d like to add a PSEdit function to your console, begin with opening your PowerShell Profile. You can even do this from PowerShell ISE, but the PowerShell ISE and the PowerShell regular console do have different profiles.
From the PowerShellISE, this will give you the location of your PowerShell ISE profile.
$profile
While this sneaky trick will give you the path to your PowerShell profile.
$Profile.Replace("PowerShellISE","PowerShell")
Most of you could also get that result with Replace(“ISE”,””), but it would fail if your user profile on your computer is in a directory with “ISE” in it. Maybe your computer account is called “Promise” or something. It could happen.
Next, load up your regular PowerShell profile instead of your ISE profile for editing using the exact same PSEdit command that we covet!
PSEdit $Profile.Replace("PowerShellISE","PowerShell")
Sweet!
Next, drop this function into your profile to create a PSEdit that will accept pipeline input, due some basic error checking. Through luck, I’ve found that using this will not open the same file if it’s already open. I didn’t even have to get fancy, it just already worked.
function PSEdit { [CmdletBinding()] param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$filename ) Process { if (test-path $filename -PathType Leaf) { $file = Get-ChildItem $filename if ($file.PSProvider.Name -eq "Filesystem") { Start-Process PowerShell_ISE -ArgumentList $file } else {Write-Error "Wrong PSProvider: $($File.PSProvider.Name)" } } else {write-error "Bad path: $filename"} } <# .SYNOPSIS Opens a file in PowerShell ISE for editing. .DESCRIPTION Accepts one or more file (as paths) and then checks them to make sure the path exists and it's a file. If any of the files are not already open in PowerShell ISE, the file is opened in a new tab. .EXAMPLE PSEdit $Filename Opens the file at $filename in the PowerShell ISE .EXAMPLE Get-ChildItem C:\Scripts\ProjectGenesis\*.ps1 | PSEdit Opens all PS1 files in the Project Genesis folder for editing. .LINK https://www.ilovepowershell.com/ #> }
I got this idea from Mike Robbins, who wrote a nice post about PSEdit on his blog. http://mikefrobbins.com/2014/05/19/open-scripts-in-a-new-tab-in-the-powershell-ise-using-psedit/
EDIT: Or, you could have just used the ISE function from the PowerShell console.