Skip to content

Instantly share code, notes, and snippets.

@glektarssza
Created May 8, 2025 16:12
Show Gist options
  • Save glektarssza/a5f5c75395833dc775a5e5aa4e331e9c to your computer and use it in GitHub Desktop.
Save glektarssza/a5f5c75395833dc775a5e5aa4e331e9c to your computer and use it in GitHub Desktop.
PowerShell Location Change Hook
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