在 PowerShell 中切换到主目录

Iai*_*der 19 windows-7 powershell

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

cd %UserProfile%
Run Code Online (Sandbox Code Playgroud)

在 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
Run Code Online (Sandbox Code Playgroud)

PowerShell 中的等效命令是什么?

imt*_*man 32

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

cd $home
Run Code Online (Sandbox Code Playgroud)

  • 作为此答案的附加内容, %UserProfile% 不会按字面意思转换为 $home,相反,您应该使用 $env:UserProfile 变量。 (5认同)

小智 14

这个速记是我的最爱之一:

cd ~
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

cd ~\Deskt 
Run Code Online (Sandbox Code Playgroud)

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