DAGDA SEARCH | Back to results | Support

Powershell 'cd' that behaves properly: zero args bring you home, passing '-' brings you to the previous dir · GitHub

Source: https://gist.github.com/albertvaka/b50974ddefdd8ff0625eb31982091034

Instantly share code, notes, and snippets.

@albertvaka

albertvaka/~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Last active June 20, 2026 11:14 Show Gist options

Select an option

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/albertvaka/b50974ddefdd8ff0625eb31982091034.js"></script> Save albertvaka/b50974ddefdd8ff0625eb31982091034 to your computer and use it in GitHub Desktop. Download ZIP Powershell 'cd' that behaves properly: zero args bring you home, passing '-' brings you to the previous dir Raw ~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 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 Show hidden characters
del alias:cd -Force # Remove builtin cd alias to Set-Location
function cd {
$pwd = Get-Location
if ($args.Count -eq 0) {
Set-Location ~
} elseif ($args[0] -eq "-") {
Set-Location @global:OLDPWD
} else {
Set-Location @args
}
$global:OLDPWD = $pwd
}
# Bonus: Make autocompletion work like on bash:
Set-PSReadlineKeyHandler -Key Tab -Function Complete
# Or like in zsh:
# Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment