gpt4 book ai didi

powershell - 在PowerShell中定义的默认别名在哪里?

转载 作者:行者123 更新时间:2023-12-04 09:43:42 25 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但是默认别名(例如cd)是在PowerShell中硬编码还是在某个位置的隐藏“配置文件”脚本中定义的?

我没有设置任何配置文件(针对每个用户或整个系统),所以我想知道默认配置文件来自何处。

最佳答案

它们是“内置的”但不是一成不变的。笔记:

PS >  (Get-Alias dir).OptionsAllScopePS >  (Get-Alias gci).OptionsReadOnly, AllScope

PS > Get-Alias | group Options

Count Name Group----- ---- ----- 91 ReadOnly, AllScope {%, ?, ac, asnp...} 46 AllScope {cat, cd, chdir, clear...}

As you can see, there is some partitioning of aliases by the ReadOnly option. The ReadOnly ones are idiomatic in PowerShell, while the mutable ones are for people familiar with other shells. I've seen people modify dir to add more functionality, while keeping gci as a straight alias to Get-ChildItem.

For broad compatability, I only use the ReadOnly aliases in my scripts.

Also, because dir in CMD, ls in UNIX, and gci in PowerShell each work in their own way, I train myself to use the native command, and not an alias. dir tends to work everywhere, but dir -Recurse does not!

As a training exercise, and to test my scripts for compatibility, I sometimes remove the non-ReadOnly aliases:

Get-Alias | ? { ! ($_.Options -match "ReadOnly") } | % { Remove-Item alias:$_ }

有一种更温和的方法,其中您用一个新命令替换每个别名,该命令会警告您您使用的是兼容性别名之一,但可以保持功能正常。

另外,如果您确实愿意,可以更改 ReadOnly别名,但是由于上述原因,我建议您反对它:

PS>别名设置-名称sl-值Get-ChildItem-强制-选项AllScope#不好!
PS> sl

目录:C:\Users\Jay

模式LastWriteTime长度名称
---- ------------- ------ ----

关于powershell - 在PowerShell中定义的默认别名在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2770526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com