Skip to content

Instantly share code, notes, and snippets.

@markwragg
Created March 5, 2020 09:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwragg/e7f92b93b05819fa64dbf34086ea87b4 to your computer and use it in GitHub Desktop.
Save markwragg/e7f92b93b05819fa64dbf34086ea87b4 to your computer and use it in GitHub Desktop.
PowerShell Profile snippet to save/return to last location on exit
$LastLocationFile = Join-Path $env:USERPROFILE 'lastlocation.txt'
If (Test-Path $LastLocationFile) {
Import-Clixml $LastLocationFile | Set-Location
}
Else {
Set-Location $env:USERPROFILE
}
Register-EngineEvent PowerShell.Exiting -Action {
Get-Location | Export-Clixml $LastLocationFile -Force
} | Out-Null
@varunswarup0
Copy link

Hey, I tried this by removing everything from profile.ps1 and adding this snippet only.
It didn't work. 😭
Can you please upload your whole profile.ps1?
Thanks.

@markwragg
Copy link
Author

It’s worth noting it only works if you end your PowerShell session by typing “exit” is that what you were doing?

@varunswarup0
Copy link

Should have read the code more carefully 😅, Thanks a lot 😉.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment