Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmcnabb/4f682ffbbcca358c2ae8 to your computer and use it in GitHub Desktop.
Save mattmcnabb/4f682ffbbcca358c2ae8 to your computer and use it in GitHub Desktop.
Powershell ISE: Automatically change working directory
$null = Register-ObjectEvent -InputObject $psISE.PowerShellTabs.SelectedPowerShellTab -EventName 'PropertyChanged' -Action {
if($args[1].PropertyName -eq 'LastEditorWithFocus' -and $env:AutoChangeLocation -eq $true)
{
$Location = Get-Location
$NewLocation = split-path $psISE.PowerShellTabs.SelectedPowerShellTab.Files.SelectedFile.FullPath
if ($Location.path -ne $NewLocation)
{
Set-Location $NewLocation
Out-Host -InputObject ' '
prompt
}
}
}
[Environment]::SetEnvironmentVariable('AutoChangeLocation',$true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment