Change to home directory in PowerShell

At the cmd command prompt, this command will take me to my home directory:

cd %UserProfile%

At the PowerShell command prompt, the same command produces this error:

Set-Location : Cannot find path 'C:\%UserProfile%' because it does not exist.
At line:1 char:3
+ cd <<<< %UserProfile% + CategoryInfo : ObjectNotFound: (C:\%UserProfile%:String) [Set-Location], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

What is the equivalent command in PowerShell?

4 Answers

You can get to your home dir with this command:

cd $home
2

This shorthand is one of my favorites:

cd ~

You can also do:

cd ~\Deskt 

(Hit the Tab key to auto-complete, it works nicely when you are buried in some deep directory and need to copy something to Desktop or somewhere in your $HOME)

3

If you are using PowerShell, you can't use %userprofile% or %homepath%. You have to use $home to change directory to home path of current user.

cd $home
1

Adding a solution for those wanting a Linux-like experience

When using a shell such as Bash, simply typing cd without parameters will take you to your home directory. To recreate this functionality in Powershell, add this to your profile:

function ChangeDirectory { param( [parameter(Mandatory=$false)] $path ) if ( $PSBoundParameters.ContainsKey('path') ) { Set-Location $path } else { Set-Location $home }
}
Remove-Item alias:\cd
New-Alias cd ChangeDirectory

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like