Created
May 8, 2025 16:12
-
-
Save glektarssza/a5f5c75395833dc775a5e5aa4e331e9c to your computer and use it in GitHub Desktop.
PowerShell Location Change Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Register-LocationChangedHandler { | |
$script:existingAction = $null; | |
if ($null -ne $ExecutionContext.InvokeCommand.LocationChangedAction) { | |
$script:existingAction = $ExecutionContext.InvokeCommand.LocationChangedAction; | |
} | |
$ExecutionContext.InvokeCommand.LocationChangedAction = { | |
param( | |
[System.Object] | |
$eventSender, | |
[System.Management.Automation.LocationChangedEventArgs] | |
$locationChangedEvent | |
) | |
#=== CUSTOM HOOK START === | |
# Do custom stuff here, probably inside a try-catch block | |
#=== CUSTOM HOOK END === | |
if ($null -ne $script:existingAction) { | |
$script:existingAction.Invoke($eventSender, $locationChangedEvent); | |
} | |
}; | |
} | |
# Call function to register | |
Register-LocationChangedHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment