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.
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.
Thanks Jeff, I’m gonna make some help for it i guess.
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
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.
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.