在Windows中,我们可以使用C:\Users\%username%,但它不适用于PowerShell和Set-Location。
有什么办法可以用一条线吗?
文件资源管理器-成功

Set-Location -失败
$set-location -path "c:\users\%username%"
set-location : Cannot find path 'C:\users\%username%' because it does not exist.
At line:1 char:1
+ set-location -path "c:\users\%username%"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~发布于 2014-05-25 01:47:12
# this will do
Set-Location -path "c:\users\$env:username\desktop"
# alternative syntax
cd "c:\users\$env:username\desktop"发布于 2014-05-25 01:49:03
在powershell中,环境变量通过$ENV:Variable访问。
例如:
Set-Location -Path "C:\Users\$ENV:UserName"或者,您可以使用C# API:
Set-Location -Path [Environment]::ExpandEnvironmentVariables("C:\Users\%Username%")发布于 2014-05-25 20:50:06
只需使用倾斜字符:
Set-Location ~或
Set-Location "~/My Documents"https://stackoverflow.com/questions/23851240
复制相似问题