23

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 4

39

You can get to your home dir with this command:

cd $home
2
  • 6
    As an add-on to this answer, %UserProfile% does not literally translate to $home, and, instead, you should use the $env:UserProfile variable.
    – SpellingD
    Jul 19, 2012 at 15:57
  • check out below answer superuser.com/a/858790/36875 Use "~" instead and save some chars!! Jul 21, 2020 at 14:59
17

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
  • Nice, somehow I didn't know you could use ~ in PowerShell! Dec 1, 2015 at 0:52
  • this should be the accepted answer. Devs are lazy and prefer to type 1 char instead of 5 :) Jul 21, 2020 at 14:58
  • @thiago, but ~ is a special key, you need the Altgr on a german key layout. I wonder why this does not work: function cd { Set-Location cd ~} which is the linux style: cdonly brings you home. My solution is to have another functionname such as:function ho { Set-Location cd ~} for home sweet home.
    – Timo
    Nov 9, 2020 at 9:42
0

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
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .