PowerShell 中定义的默认别名在哪里? [英] Where are the default aliases defined in PowerShell?
问题描述
这可能是一个愚蠢的问题,但默认别名(例如 cd)是在 PowerShell 中硬编码还是在某个隐藏的配置文件"脚本中定义?
This may be a stupid question, but are the default aliases (e.g. cd) hardcoded in PowerShell or defined in a hidden "profile" script somewhere?
我没有设置任何配置文件(每个用户或系统范围),所以我只是想知道默认配置文件来自哪里.
I don't have any profiles set (per-user or system-wide) so I'm just wondering where the default ones come from.
推荐答案
它们是内置"的,但不是一成不变的.注意:
They are "built in" but not immutable. Note:
PS > (Get-Alias dir).Options全范围PS > (Get-Alias gci).Options只读,AllScopePS > (Get-Alias dir).Options AllScope PS > (Get-Alias gci).Options ReadOnly, AllScopePS > 获取别名 |组选项
PS > Get-Alias | group Options
计数名称组----- ---- -----91 ReadOnly, AllScope {%, ?, ac, asnp...}46 AllScope {cat, cd, chdir, clear...}
Count Name Group ----- ---- ----- 91 ReadOnly, AllScope {%, ?, ac, asnp...} 46 AllScope {cat, cd, chdir, clear...}
如您所见,
ReadOnly
选项对别名进行了一些分区.ReadOnly
在 PowerShell 中是惯用的,而可变的用于熟悉其他 shell 的人.我见过有人修改dir
以添加更多功能,同时将gci
作为Get-ChildItem
的直接别名.As you can see, there is some partitioning of aliases by the
ReadOnly
option. TheReadOnly
ones are idiomatic in PowerShell, while the mutable ones are for people familiar with other shells. I've seen people modifydir
to add more functionality, while keepinggci
as a straight alias toGet-ChildItem
.为了广泛的兼容性,我只在我的脚本中使用
ReadOnly
别名.For broad compatability, I only use the
ReadOnly
aliases in my scripts.此外,因为 CMD 中的
dir
、UNIX 中的ls
和 PowerShell 中的gci
都以自己的方式工作,所以我训练自己使用本机命令,而不是别名.dir
往往适用于任何地方,但dir -Recurse
不适用!Also, because
dir
in CMD,ls
in UNIX, andgci
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, butdir -Recurse
does not!作为训练练习,并测试我的脚本的兼容性,我有时会删除非
ReadOnly
别名: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:$_ }
有一种更温和的方法,您可以将每个别名替换为一个新命令,该命令会警告您正在使用兼容别名之一,但可以让您继续运行.
There's a more gentle approach where you replace each alias with a new command that warns you that you're using one of the compatibility aliases, but lets you keep functioning.
此外,如果您真的愿意,您可以更改
ReadOnly
别名,但由于上述原因,我建议您不要这样做:Also, you can change the
ReadOnly
aliases if you really want to, but for the above reasons I'd recommend against it:PS > Set-Alias -Name sl -Value Get-ChildItem -Force -Option AllScope # BAD!PS > slPS > Set-Alias -Name sl -Value Get-ChildItem -Force -Option AllScope # BAD! PS > sl目录:C:\Users\Jay
Directory: C:\Users\Jay
模式 LastWriteTime 长度名称---- ------------- ------ ----
Mode LastWriteTime Length Name ---- ------------- ------ ----
这篇关于PowerShell 中定义的默认别名在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!