Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: A Better PSEdit

Posted on May 29, 2015May 29, 2015

In the PowerShell ISE, there is a built-in function called PSEdit. You can use this function to easily load a file in to the ISE directly from the ISE command prompt.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Psedit c:\scripts\myscript.ps1

You can also load multiple files, but not as easily as you might like. I find myself wanting to do this:

As you can see isn't what I'm expecting. I can get PSEdit to open multiple files, but I need to use a command like this:

psedit (dir vAL*.PS1)

I finally tired of this so I looked at the code for the PSEdit function.

I am assuming based on what I see that this was written a long time ago. So I decided to update it. Here's my version:

#requires -version 4.0

Function PSEdit2 {
[cmdletbinding()]

param(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,
HelpMessage="Enter a file path")]
[Alias("Fullname","Filenames")]
[ValidateScript({
if (Test-Path $_) {
$True
}
else {
Throw "Cannot validate path $_."
}
})]
[string[]]$Path
)

Begin {
   Write-Verbose "Starting $($MyInvocation.Mycommand)"
} #begin

Process {
   foreach ($filename in $path) {
     #resolve paths
     $resolved = Resolve-Path -Path $filename
     Write-Verbose "Opening $resolved"
     $psISE.CurrentPowerShellTab.Files.Add($resolved) | Out-Null
}

} #process
End {
  Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end

} #end function

The major difference is that my version works in the pipeline making it easier, for me at least, to open multiple files at once.

dir val*.ps1 | psedit2

I also added some verbose messages for troubleshooting. This is my common practice when creating new PowerShell tools. You'll also notice that I replaced the aliases in the original function with complete cmdlet and parameter names.

The last "feature" is my customized ValidateScript attribute. I wanted to verify that any path pointed to a legitimate file. I could have simply used this:

[ValidateScript({Test-Path $_})]

But if the path failed the test, PowerShell displays a long error message that isn't always helpful. So I added some logic. Validation tests have to return either True or False. When it is false, PowerShell throws the exception. So I wrote my own exception message.

I get a similar error with the original psedit.

So perhaps I haven't improved on it that much. But I could have written an even longer message and I wanted to demonstrate this technique in case you wanted to use it.

One last word on my version of PSEdit. I didn't use a standard name, I guess because the original function doesn't use one. And I'm ok with that. This is one of the situations where the function is a "cheater" command with a simple, alias-like, name. If you want to replace the original PSEdit function, add mine to your ISE profile script and rename it to PSEdit.

Enjoy.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

4 thoughts on “Friday Fun: A Better PSEdit”

  1. Jim Beam says:
    May 31, 2015 at 11:29 pm

    Thanks Jeff, I’m gonna make some help for it i guess.

  2. Jay says:
    June 2, 2015 at 9:21 am

    Jeff, I saw Snover do this at the Unplugged session at Ignite this year and I jotted it down.

    psedit *

    This opens all files in the current directory

    I also tested with
    psedit .
    psedit val*

    I think this is the designed way to open multiple files

    Cheers

    @jaywryan

    1. Jeffery Hicks says:
      June 2, 2015 at 9:55 am

      Yes it does. Sometimes, I don’t know what I want to open until I run a DIR command. And if you needed anything more complicated such as all val*.ps1 files modified in the last 7 days, psedit gets cumbersome. Don’t get me wrong, I love the shortcut. My Friday Fun posts are also supposed to be educational so the process is just as important as the end result. Thanks for taking the time to comment.

      1. Jay says:
        June 2, 2015 at 10:06 am

        Thanks for the quick reply. I read them every week and learn something every time! Keep it up! Thanks for your contributions to the community.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d