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.SetLocationCommandWhat 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)
3If 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