如何制作别名表

如何制作别名表

我们知道,aCmtlet可能有很多别名,例如copy,cp,cpialways 意思Copy-Item。所以我想创建一个别名表,它会像:

...
...
...
Copy-Item     copy,cp,cpi
Clear-Host    cls,clear
Move-Item     mi,move,mv
...
...
...

我可以获取用于获取第一列的所有别名信息..但(Get-Command -CommandType Alias).Definition|Get-Unique我不知道如何获取第二列。

答案1

如果您希望它按顺序排列并且不带花括号::

Get-Alias | Sort Definition | Group-Object Definition|
  ForEach-Object{"{0,-30} {1}" -f $_.Name,(($_.group.name) -join(', ')) }

Add-Content                    ac
Add-PSSnapIn                   asnp
Clear-Content                  clc
Clear-History                  clhy
Clear-Host                     clear, cls

编辑[pscustomobject]返回格式化字符串的变体:

Get-Alias | Sort-Object Definition | Group-Object Definition | ForEach-Object{
    [PSCustomObject]@{
        CmdLet = $_.Name
        Aliases =(($_.group.name) -join(', '))
    }
}

CmdLet                         Aliases
------                         -------
Add-Content                    ac
Add-PSSnapIn                   asnp
Clear-Content                  clc
Clear-History                  clhy
Clear-Host                     clear, cls

答案2

PS C:\> Get-Alias | Group-Object definition|ft name,group

会给予我想要的

Name                           Group
----                           -----
ForEach-Object                 {%, foreach}
Where-Object                   {?, where}
Add-Content                    {ac}
Add-PSSnapIn                   {asnp}
Get-Content                    {cat, gc, type}
Set-Location                   {cd, chdir, sl}
ConvertFrom-String             {CFS}
Clear-Content                  {clc}
Clear-Host                     {clear, cls}
Clear-History                  {clhy}
Clear-Item                     {cli}
Clear-ItemProperty             {clp}
Clear-Variable                 {clv}
Connect-PSSession              {cnsn}
Compare-Object                 {compare, diff}
...

相关内容