During my day I may be working on multiple files in the PowerShell ISE. Often these files are part of different modules and since I use git I generally need to be in the same directory as the file I'm working on. This necessitates a lot of manually changing location in the PowerShell ISE prompt. Or I might open the PowerShell ISE and then select some files from MRU list. But again, if I need to jump to the corresponding directory, that can be a tedious process.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Here's an example.
I'm working on my memory tools module and you can see the path. If my ISE prompt is currently C:\ I have to discover the path to the file, which I can do by hovering on the tab, and then use Set-Location or the cd alias to change directories. There needs to be a better way. Here is mine.
The function is quite simple. It gets the filename of the currently active file in the PowerShell ISE and changes the directory location to the script's parent folder. I also save the current location to the location stack with Push-Location. The benefit is that if you need to jump to a previous location you can use Pop-Location. You'll see that in action in a moment.
Now, you could load this function into your PowerShell ISE profile and add a shortcut to the Add-Ons menu. But I figured since I'm most likely already at the console prompt ready to work to simply create an alias.
Here's an example of what you can expect between selecting different files in the ISE.
Notice that you can use the popd alias (for Pop-Location) to jump back to previous locations.
I'm using the function like this in my PowerShell ISE profile.
. C:\scripts\Set-ScriptLocation.ps1
But you are more than welcome to adjust the function as you need. I hope you'll let me know how this works out for you. Use GitHub to report any problems or requests.