这可能是一个愚蠢的问题,但是在PowerShell中是硬编码的默认别名(例如cd)还是在某个隐藏的“配置文件”脚本中定义的?
我没有设置任何配置文件(按用户或系统范围),所以我只是想知道默认设置来自哪里。
答案 0 :(得分:10)
它们是“内置的”但不是一成不变的。注意:
PS > (Get-Alias dir).Options AllScope PS > (Get-Alias gci).Options ReadOnly, AllScopePS > Get-Alias | group Options
Count Name Group ----- ---- ----- 91 ReadOnly, AllScope {%, ?, ac, asnp...} 46 AllScope {cat, cd, chdir, clear...}
如您所见,ReadOnly
选项对别名进行了一些分区。 ReadOnly
在PowerShell中是惯用的,而可变的则是熟悉其他shell的人。我看到有人修改dir
以添加更多功能,同时将gci
保留为Get-ChildItem
的直接别名。
为了广泛的兼容性,我只在脚本中使用ReadOnly
别名。
另外,因为CMD中的dir
,UNIX中的ls
和PowerShell中的gci
都以自己的方式工作,所以我训练自己使用本机命令,而不是别名。 dir
往往适用于所有地方,但dir -Recurse
没有!
作为一项培训练习,为了测试我的脚本的兼容性,我有时会删除非ReadOnly
别名:
Get-Alias | ? { ! ($_.Options -match "ReadOnly") } | % { Remove-Item alias:$_ }
有一个更温和的方法,你用一个新命令替换每个别名,警告你正在使用其中一个兼容性别名,但让你继续运作。
此外,如果您真的想要,可以更改ReadOnly
别名,但由于上述原因,我建议您不要这样做:
PS > Set-Alias -Name sl -Value Get-ChildItem -Force -Option AllScope # BAD! PS > slDirectory: C:\Users\Jay
Mode LastWriteTime Length Name ---- ------------- ------ ----
答案 1 :(得分:7)
硬编码,但可检索(与PowerShell中的大多数“隐藏”)
PS> [Management.Automation.Runspaces.InitialSessionState].getproperty(
"BuiltInAliases", [reflection.bindingflags]"NonPublic,Static").getvalue(
$null, @()) | format-table -auto
Definition Description Options CommandType Visibility Name PSSnapIn Module
---------- ----------- ------- ----------- ---------- ---- -------- ------
Add-Content ReadOnly, AllScope Alias Public ac
Add-PSSnapIn ReadOnly, AllScope Alias Public asnp
Clear-Content ReadOnly, AllScope Alias Public clc
Clear-Item ReadOnly, AllScope Alias Public cli
Clear-ItemProperty ReadOnly, AllScope Alias Public clp
Clear-Variable ReadOnly, AllScope Alias Public clv
...
- )
答案 2 :(得分:1)
虽然我不知道技术细节,但我会说它们是硬编码的,而且它们是不可配置的。它们可以重新定义或删除,但初始设置不在我们的控制范围内。
答案 3 :(得分:1)
通过使用来自GitHub的(C#)源代码查找内置别名的另一种方法:
internal static SessionStateAliasEntry[] BuiltInAliases {
get {
// Too many AllScope entries hurts performance because an entry is
// created in each new scope, so we limit the use of AllScope to the
// most commonly used commands - primarily so command lookup is faster,
// though if we speed up command lookup significantly, then removing
// AllScope for all of these aliases makes sense.
const ScopedItemOptions AllScope = ScopedItemOptions.AllScope;
const ScopedItemOptions ReadOnly_AllScope = ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope;
const ScopedItemOptions ReadOnly = ScopedItemOptions.ReadOnly;
return new SessionStateAliasEntry[] {
new SessionStateAliasEntry("foreach", "ForEach-Object", string.Empty, ReadOnly_AllScope),
new SessionStateAliasEntry("%", "ForEach-Object", string.Empty, ReadOnly_AllScope),
new SessionStateAliasEntry("where", "Where-Object", string.Empty, ReadOnly_AllScope),
new SessionStateAliasEntry("?", "Where-Object", string.Empty, ReadOnly_AllScope),
new SessionStateAliasEntry("clc", "Clear-Content", string.Empty, ReadOnly),
new SessionStateAliasEntry("cli", "Clear-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("clp", "Clear-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("clv", "Clear-Variable", string.Empty, ReadOnly),
new SessionStateAliasEntry("cpi", "Copy-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("cvpa", "Convert-Path", string.Empty, ReadOnly),
new SessionStateAliasEntry("dbp", "Disable-PSBreakpoint", string.Empty, ReadOnly),
new SessionStateAliasEntry("ebp", "Enable-PSBreakpoint", string.Empty, ReadOnly),
new SessionStateAliasEntry("epal", "Export-Alias", string.Empty, ReadOnly),
new SessionStateAliasEntry("epcsv", "Export-Csv", string.Empty, ReadOnly),
new SessionStateAliasEntry("fl", "Format-List", string.Empty, ReadOnly),
new SessionStateAliasEntry("ft", "Format-Table", string.Empty, ReadOnly),
new SessionStateAliasEntry("fw", "Format-Wide", string.Empty, ReadOnly),
new SessionStateAliasEntry("gal", "Get-Alias", string.Empty, ReadOnly),
new SessionStateAliasEntry("gbp", "Get-PSBreakpoint", string.Empty, ReadOnly),
new SessionStateAliasEntry("gc", "Get-Content", string.Empty, ReadOnly),
new SessionStateAliasEntry("gci", "Get-ChildItem", string.Empty, ReadOnly),
new SessionStateAliasEntry("gcm", "Get-Command", string.Empty, ReadOnly),
new SessionStateAliasEntry("gdr", "Get-PSDrive", string.Empty, ReadOnly),
new SessionStateAliasEntry("gcs", "Get-PSCallStack", string.Empty, ReadOnly),
new SessionStateAliasEntry("ghy", "Get-History", string.Empty, ReadOnly),
new SessionStateAliasEntry("gi", "Get-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("gl", "Get-Location", string.Empty, ReadOnly),
new SessionStateAliasEntry("gm", "Get-Member", string.Empty, ReadOnly),
new SessionStateAliasEntry("gmo", "Get-Module", string.Empty, ReadOnly),
new SessionStateAliasEntry("gp", "Get-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("gpv", "Get-ItemPropertyValue", string.Empty,ReadOnly),
new SessionStateAliasEntry("gps", "Get-Process", string.Empty, ReadOnly),
new SessionStateAliasEntry("group", "Group-Object", string.Empty, ReadOnly),
new SessionStateAliasEntry("gu", "Get-Unique", string.Empty, ReadOnly),
new SessionStateAliasEntry("gv", "Get-Variable", string.Empty, ReadOnly),
new SessionStateAliasEntry("iex", "Invoke-Expression", string.Empty, ReadOnly),
new SessionStateAliasEntry("ihy", "Invoke-History", string.Empty, ReadOnly),
new SessionStateAliasEntry("ii", "Invoke-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("ipmo", "Import-Module", string.Empty, ReadOnly),
new SessionStateAliasEntry("ipal", "Import-Alias", string.Empty, ReadOnly),
new SessionStateAliasEntry("ipcsv", "Import-Csv", string.Empty, ReadOnly),
new SessionStateAliasEntry("measure", "Measure-Object", string.Empty, ReadOnly),
new SessionStateAliasEntry("mi", "Move-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("mp", "Move-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("nal", "New-Alias", string.Empty, ReadOnly),
new SessionStateAliasEntry("ndr", "New-PSDrive", string.Empty, ReadOnly),
new SessionStateAliasEntry("ni", "New-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("nv", "New-Variable", string.Empty, ReadOnly),
new SessionStateAliasEntry("nmo", "New-Module", string.Empty, ReadOnly),
new SessionStateAliasEntry("oh", "Out-Host", string.Empty, ReadOnly),
new SessionStateAliasEntry("rbp", "Remove-PSBreakpoint", string.Empty, ReadOnly),
new SessionStateAliasEntry("rdr", "Remove-PSDrive", string.Empty, ReadOnly),
new SessionStateAliasEntry("ri", "Remove-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("rni", "Rename-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("rnp", "Rename-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("rp", "Remove-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("rmo", "Remove-Module", string.Empty, ReadOnly),
new SessionStateAliasEntry("rv", "Remove-Variable", string.Empty, ReadOnly),
new SessionStateAliasEntry("rvpa", "Resolve-Path", string.Empty, ReadOnly),
new SessionStateAliasEntry("sal", "Set-Alias", string.Empty, ReadOnly),
new SessionStateAliasEntry("sbp", "Set-PSBreakpoint", string.Empty, ReadOnly),
new SessionStateAliasEntry("select", "Select-Object", string.Empty, ReadOnly_AllScope),
new SessionStateAliasEntry("si", "Set-Item", string.Empty, ReadOnly),
new SessionStateAliasEntry("sl", "Set-Location", string.Empty, ReadOnly),
new SessionStateAliasEntry("sp", "Set-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("saps", "Start-Process", string.Empty, ReadOnly),
new SessionStateAliasEntry("spps", "Stop-Process", string.Empty, ReadOnly),
new SessionStateAliasEntry("sv", "Set-Variable", string.Empty, ReadOnly),
// Web cmdlets aliases
new SessionStateAliasEntry("irm", "Invoke-RestMethod", string.Empty, ReadOnly),
new SessionStateAliasEntry("iwr", "Invoke-WebRequest", string.Empty, ReadOnly),
// Porting note: #if !UNIX is used to disable aliases for cmdlets which conflict with Linux / macOS
#if !UNIX
// ac is a native command on macOS
new SessionStateAliasEntry("ac", "Add-Content", string.Empty, ReadOnly),
new SessionStateAliasEntry("compare", "Compare-Object", string.Empty, ReadOnly),
new SessionStateAliasEntry("cpp", "Copy-ItemProperty", string.Empty, ReadOnly),
new SessionStateAliasEntry("diff", "Compare-Object", string.Empty, ReadOnly),
new SessionStateAliasEntry("gsv", "Get-Service", string.Empty, ReadOnly),
new SessionStateAliasEntry("sleep", "Start-Sleep", string.Empty, ReadOnly),
new SessionStateAliasEntry("sort", "Sort-Object", string.Empty, ReadOnly),
new SessionStateAliasEntry("start", "Start-Process", string.Empty, ReadOnly),
new SessionStateAliasEntry("sasv", "Start-Service", string.Empty, ReadOnly),
new SessionStateAliasEntry("spsv", "Stop-Service", string.Empty, ReadOnly),
new SessionStateAliasEntry("tee", "Tee-Object", string.Empty, ReadOnly),
new SessionStateAliasEntry("write", "Write-Output", string.Empty, ReadOnly),
// These were transferred from the "transferred from the profile" section
new SessionStateAliasEntry("cat", "Get-Content"),
new SessionStateAliasEntry("cp", "Copy-Item", string.Empty, AllScope),
new SessionStateAliasEntry("ls", "Get-ChildItem"),
new SessionStateAliasEntry("man", "help"),
new SessionStateAliasEntry("mount", "New-PSDrive"),
new SessionStateAliasEntry("mv", "Move-Item"),
new SessionStateAliasEntry("ps", "Get-Process"),
new SessionStateAliasEntry("rm", "Remove-Item"),
new SessionStateAliasEntry("rmdir", "Remove-Item"),
new SessionStateAliasEntry("cnsn", "Connect-PSSession", string.Empty, ReadOnly),
new SessionStateAliasEntry("dnsn", "Disconnect-PSSession", string.Empty, ReadOnly),
#endif
// Bash built-ins we purposefully keep even if they override native commands
new SessionStateAliasEntry("cd", "Set-Location", string.Empty, AllScope),
new SessionStateAliasEntry("dir", "Get-ChildItem", string.Empty, AllScope),
new SessionStateAliasEntry("echo", "Write-Output", string.Empty, AllScope),
new SessionStateAliasEntry("fc", "Format-Custom", string.Empty, ReadOnly),
new SessionStateAliasEntry("kill", "Stop-Process"),
new SessionStateAliasEntry("pwd", "Get-Location"),
new SessionStateAliasEntry("type", "Get-Content"),
// Native commands we keep because the functions act correctly on Linux
new SessionStateAliasEntry("clear", "Clear-Host"),
// #if !CORECLR is used to disable aliases for cmdlets which are not available on OneCore or not appropriate for PSCore6 due to conflicts
#if !CORECLR
new SessionStateAliasEntry("gwmi", "Get-WmiObject", string.Empty, ReadOnly),
new SessionStateAliasEntry("iwmi", "Invoke-WMIMethod", string.Empty, ReadOnly),
new SessionStateAliasEntry("ogv", "Out-GridView", string.Empty, ReadOnly),
new SessionStateAliasEntry("ise", "powershell_ise.exe", string.Empty, ReadOnly),
new SessionStateAliasEntry("rwmi", "Remove-WMIObject", string.Empty, ReadOnly),
new SessionStateAliasEntry("sc", "Set-Content", string.Empty, ReadOnly),
new SessionStateAliasEntry("swmi", "Set-WMIInstance", string.Empty, ReadOnly),
new SessionStateAliasEntry("shcm", "Show-Command", string.Empty, ReadOnly),
new SessionStateAliasEntry("trcm", "Trace-Command", string.Empty, ReadOnly),
new SessionStateAliasEntry("lp", "Out-Printer"),
#endif
// Aliases transferred from the profile
new SessionStateAliasEntry("h", "Get-History"),
new SessionStateAliasEntry("history", "Get-History"),
new SessionStateAliasEntry("md", "mkdir", string.Empty, AllScope),
new SessionStateAliasEntry("popd", "Pop-Location", string.Empty, AllScope),
new SessionStateAliasEntry("pushd", "Push-Location", string.Empty, AllScope),
new SessionStateAliasEntry("r", "Invoke-History"),
new SessionStateAliasEntry("cls", "Clear-Host"),
new SessionStateAliasEntry("chdir", "Set-Location"),
new SessionStateAliasEntry("copy", "Copy-Item", string.Empty, AllScope),
new SessionStateAliasEntry("del", "Remove-Item", string.Empty, AllScope),
new SessionStateAliasEntry("erase", "Remove-Item"),
new SessionStateAliasEntry("move", "Move-Item", string.Empty, AllScope),
new SessionStateAliasEntry("rd", "Remove-Item"),
new SessionStateAliasEntry("ren", "Rename-Item"),
new SessionStateAliasEntry("set", "Set-Variable"),
new SessionStateAliasEntry("icm", "Invoke-Command"),
new SessionStateAliasEntry("clhy", "Clear-History", string.Empty, ReadOnly),
// Job Specific aliases
new SessionStateAliasEntry("gjb", "Get-Job"),
new SessionStateAliasEntry("rcjb", "Receive-Job"),
new SessionStateAliasEntry("rjb", "Remove-Job"),
new SessionStateAliasEntry("sajb", "Start-Job"),
new SessionStateAliasEntry("spjb", "Stop-Job"),
new SessionStateAliasEntry("wjb", "Wait-Job"),
#if !CORECLR
new SessionStateAliasEntry("sujb", "Suspend-Job"),
new SessionStateAliasEntry("rujb", "Resume-Job"),
// Remoting Cmdlets Specific aliases
new SessionStateAliasEntry("npssc", "New-PSSessionConfigurationFile", string.Empty, ReadOnly),
new SessionStateAliasEntry("ipsn", "Import-PSSession"),
new SessionStateAliasEntry("epsn", "Export-PSSession"),
#endif
new SessionStateAliasEntry("nsn", "New-PSSession"),
new SessionStateAliasEntry("gsn", "Get-PSSession"),
new SessionStateAliasEntry("rsn", "Remove-PSSession"),
new SessionStateAliasEntry("etsn", "Enter-PSSession"),
new SessionStateAliasEntry("rcsn", "Receive-PSSession", string.Empty, ReadOnly),
new SessionStateAliasEntry("exsn", "Exit-PSSession"),
// Win8: 121662/169179 Add "sls" alias for Select-String cmdlet
// - do not use AllScope - this causes errors in profiles that set this somewhat commonly used alias.
new SessionStateAliasEntry("sls", "Select-String"),
};
}
}
当然,上面的别名可能会更改,但是在某些情况下以这种方式访问它们可能会更方便/提供更多信息。
答案 4 :(得分:0)
他们是硬编码的。
答案 5 :(得分:0)
还要注意,有两个地方需要注意别名。全局参数和特定于cmdlet参数的参数。
# Get named aliases
Get-Alias |
Out-GridView -PassThru -Title 'Available aliases'
# Get cmdlet / function parameter aliases
(Get-Command Get-ADUser).Parameters.Values |
where aliases |
select Name, Aliases |
Out-GridView -PassThru -Title 'Alias results for a given cmdlet or function.'