开发者

How to invoke powershell Set-Location command silently?

开发者 https://www.devze.com 2023-04-10 16:30 出处:网络
I would like to cd to some directory without the path to that directory being displayed by powershell. No matter what I try - nothing works:

I would like to cd to some directory without the path to that directory being displayed by powershell. No matter what I try - nothing works:

PS C:\dev\windows\nc> cd .\NC.Server.Host | Out-Null
C:\dev\windows\nc\NC.Server.Host
PS C:\dev\windows\nc\NC.Server.Host> cd .. > $null
C:\dev\windows\nc
PS C:\dev\windows\nc> cd开发者_C百科 .\NC.Server.Host 2> $null
C:\dev\windows\nc\NC.Server.Host
PS C:\dev\windows\nc\NC.Server.Host> $x=cd ..
C:\dev\windows\nc
PS C:\dev\windows\nc>

As you can see the target directory path is always displayed. Is it a way to avoid it? Thanks.

EDIT

Here is my prompt function:

PS Z:\dev\3rd_party> gc Function:prompt
$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '


You probably use the PowerShell Community Extensions. In those cd is defined as an alias to Set-LocationEx which exhibits this behaviour:

PS Home:\> Set-Location ..
PS Home:\> Set-LocationEx ..
Home:\

Either use Set-Location instead of cd, or don't load PSCX or redefine cd to its usual alias after loading PSCX in your profile.

(Those are the reasons I load PSCX only occasionally if I actually need a command from there; redefining core commands or aliases with slightly different semantics is evil and should never be done.)

0

精彩评论

暂无评论...
验证码 换一张
取 消