Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute PowerShell function on change of path

Tags:

powershell

Is it possible to execute some code (eg. a function, script block, cmdlet etc) whenever the current path changes in the PowerShell console or ISE?

The scenario I am thinking of is to modify some environment variables and dot source some location-specific PowerShell functions depending on the current folder.

like image 432
David Gardiner Avatar asked Jan 30 '26 01:01

David Gardiner


1 Answers

You have several options. You can remove the "cd" alias and then write a "cd" function and add the desired logic. The downside to this approach is that if someone uses Set-Location, your function is bypassed. Another option is to create a proxy command for Set-Location. Shay (and Kirk) have a video on how to do this here. The nice thing about this approach is that the built-in ways to change dir (cd and Set-Location) will go through your proxy command.

like image 105
Keith Hill Avatar answered Jan 31 '26 19:01

Keith Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!