在PowerShell中更改到主目录


15

在cmd命令提示符下,此命令会将我带到主目录:

cd %UserProfile%

在PowerShell命令提示符处,同一命令会产生此错误:

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

PowerShell中的等效命令是什么?

Answers:


28

您可以使用以下命令进入您的主目录:

cd $home

5
作为此答案的附加组件,%UserProfile%不会从字面上转换为$ home,而是应使用$ env:UserProfile变量。
SpellingD

11

该速记是我的最爱之一:

cd ~

您也可以这样做:

cd ~\Deskt 

(按Tab一下自动完成的键,当您将其埋在某个深目录中并且需要将某些内容复制到桌面或$ HOME中的某个位置时,它会很好地工作)


很好,我不知道您可以~在PowerShell中使用它!
Nate Barbettini 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.