cmdlet 的名称由一个动词和一个名词组成,其功能对用户来讲一目了然。但是对于一个经常使用powershell命令的人每天敲那么多命令也很麻烦啊。能不能把命令缩短一点呢?于是“别名”就应运而生了。Powershell内部也实现了很多常用命令的别名。例如Get-ChildItem,列出当前的子文件或目录。它有两个别名:ls 和 dir,这两个别名来源于unix 的shell和windows的cmd。
因此别名有两个作用:

  • 继承:继承unix-shell和windows-cmd。
  • 方便:方便用户使用。

查询别名

查询别名所指的真实cmdlet命令

  1. PS C:\PS> Get-Alias -name ls
  2. CommandType Name Definition
  3. ----------- ---- ----------
  4. Alias ls Get-ChildItem
  5. PS C:\PS> Get-Alias -name dir
  6. CommandType Name Definition
  7. ----------- ---- ----------
  8. Alias dir Get-ChildItem
  9. PS C:\PS> Get-Alias -name fl
  10. CommandType Name Definition
  11. ----------- ---- ----------
  12. Alias fl Format-List
  13. PS C:\PS> Get-Alias -name ft
  14. CommandType Name Definition
  15. ----------- ---- ----------
  16. Alias ft Format-Table

查看可用的别名

查看可用的别名,可以通过” ls alias:” 或者 ”Get-Alias“

如何查看所有以Remove打头的cmdlet的命令的别名呢?

  1. PS C:\PS> dir alias: | where {$_.Definition.Startswith("Remove")}
  2. CommandType Name Definition
  3. ----------- ---- ----------
  4. Alias del Remove-Item
  5. Alias erase Remove-Item
  6. Alias rbp Remove-PSBreakpoint
  7. Alias rd Remove-Item
  8. Alias rdr Remove-PSDrive
  9. Alias ri Remove-Item
  10. Alias rjb Remove-Job
  11. Alias rm Remove-Item
  12. Alias rmdir Remove-Item
  13. Alias rmo Remove-Module
  14. Alias rp Remove-ItemProperty
  15. Alias rsn Remove-PSSession
  16. Alias rsnp Remove-PSSnapin
  17. Alias rv Remove-Variable
  18. Alias rwmi Remove-WMIObject

说明:dir alias:获取的是别名的数组,通过where对数组元素进行遍历,$_代表当前元素,alias的Definition为String类型,因为powershell支持.net,.net中的string类有一个方法Startswith。通过where过滤集合在powershell中使用非常广泛。

有的cmdlet命令可能有2-3个别名,我们可以通过下面的命令查看所有别名和指向cmdlet的别名的个数。

  1. PS C:\PS> ls alias: | Group-Object definition | sort -Descending Count
  2. Count Name Group
  3. ----- ---- -----
  4. 6 Remove-Item {del, erase, rd, ri...}
  5. 3 Set-Location {cd, chdir, sl}
  6. 3 Get-History {ghy, h, history}
  7. 3 Get-ChildItem {dir, gci, ls}
  8. 3 Get-Content {cat, gc, type}
  9. 3 Move-Item {mi, move, mv}
  10. 3 Copy-Item {copy, cp, cpi}
  11. 2 Start-Process {saps, start}
  12. 2 Set-Variable {set, sv}
  13. 2 Write-Output {echo, write}
  14. 2 Get-Process {gps, ps}
  15. 2 Invoke-History {ihy, r}
  16. 2 New-PSDrive {mount, ndr}
  17. 2 Stop-Process {kill, spps}
  18. 2 Rename-Item {ren, rni}
  19. 2 Get-Location {gl, pwd}
  20. 2 Compare-Object {compare, diff}
  21. 2 Where-Object {?, where}
  22. 2 ForEach-Object {%, foreach}
  23. 2 Clear-Host {clear, cls}
  24. 1 Out-Host {oh}
  25. 1 New-PSSession {nsn}
  26. 1 New-Variable {nv}
  27. 1 Out-GridView {ogv}
  28. 1 Pop-Location {popd}
  29. 1 Tee-Object {tee}
  30. 1 Remove-PSBreakpoint {rbp}
  31. 1 Receive-Job {rcjb}
  32. 1 Push-Location {pushd}
  33. 1 mkdir {md}
  34. 1 Measure-Object {measure}
  35. 1 help {man}
  36. 1 Remove-PSSnapin {rsnp}
  37. 1 Out-Printer {lp}
  38. 1 New-Item {ni}
  39. 1 New-Module {nmo}
  40. 1 New-Alias {nal}
  41. 1 Move-ItemProperty {mp}
  42. 1 Wait-Job {wjb}
  43. 1 Remove-PSDrive {rdr}
  44. 1 Start-Service {sasv}
  45. 1 Set-PSBreakpoint {sbp}
  46. 1 Set-ItemProperty {sp}
  47. 1 Start-Job {sajb}
  48. 1 Set-Alias {sal}
  49. 1 Start-Sleep {sleep}
  50. 1 Set-Item {si}
  51. 1 Select-Object {select}
  52. 1 Set-Content {sc}
  53. 1 Sort-Object {sort}
  54. 1 Remove-WMIObject {rwmi}
  55. 1 Remove-Module {rmo}
  56. 1 Rename-ItemProperty {rnp}
  57. 1 Stop-Service {spsv}
  58. 1 Set-WMIInstance {swmi}
  59. 1 Remove-Job {rjb}
  60. 1 Remove-Variable {rv}
  61. 1 Resolve-Path {rvpa}
  62. 1 Stop-Job {spjb}
  63. 1 Remove-ItemProperty {rp}
  64. 1 Remove-PSSession {rsn}
  65. 1 Exit-PSSession {exsn}
  66. 1 Format-Custom {fc}
  67. 1 Enter-PSSession {etsn}
  68. 1 Export-Csv {epcsv}
  69. 1 Export-PSSession {epsn}
  70. 1 Format-List {fl}
  71. 1 Get-PSBreakpoint {gbp}
  72. 1 Get-Command {gcm}
  73. 1 Get-Alias {gal}
  74. 1 Format-Table {ft}
  75. 1 Format-Wide {fw}
  76. 1 Export-Alias {epal}
  77. 1 Clear-History {clhy}
  78. 1 Clear-Item {cli}
  79. 1 Clear-Content {clc}
  80. 1 Add-Content {ac}
  81. 1 Add-PSSnapIn {asnp}
  82. 1 Clear-ItemProperty {clp}
  83. 1 Disable-PSBreakpoint {dbp}
  84. 1 Enable-PSBreakpoint {ebp}
  85. 1 Convert-Path {cvpa}
  86. 1 Clear-Variable {clv}
  87. 1 Copy-ItemProperty {cpp}
  88. 1 Invoke-Expression {iex}
  89. 1 Invoke-Item {ii}
  90. 1 Invoke-Command {icm}
  91. 1 Get-Variable {gv}
  92. 1 Get-WmiObject {gwmi}
  93. 1 Import-Alias {ipal}
  94. 1 powershell_ise.exe {ise}
  95. 1 Invoke-WMIMethod {iwmi}
  96. 1 Import-PSSession {ipsn}
  97. 1 Import-Csv {ipcsv}
  98. 1 Import-Module {ipmo}
  99. 1 Get-Unique {gu}
  100. 1 Get-Job {gjb}
  101. 1 Get-Member {gm}
  102. 1 Get-Item {gi}
  103. 1 Get-PSCallStack {gcs}
  104. 1 Get-PSDrive {gdr}
  105. 1 Get-Module {gmo}
  106. 1 Get-PSSnapIn {gsnp}
  107. 1 Get-Service {gsv}
  108. 1 Get-PSSession {gsn}
  109. 1 Get-ItemProperty {gp}
  110. 1 Group-Object {group}

通过简写查看全部别名:

  1. PS C:\PS> gal

查看指定别名对应的命令:

  1. PS C:\PS> gal ls

创建自己的别名

给记事本创建一个别名,并查看该别名;

  1. PS C:\PS> Set-Alias -Name Edit -Value notepad
  2. PS C:\PS> Edit
  3. PS C:\PS> $alias:Edit
  4. notepad

也可以通过以下形式创建别名(不带-Name-Value):

  1. Set-Alias edit notepad

删除自己的别名

别名不用删除,自定义的别名在powershell退出时会自动清除。但是请放心,powershell内置别名(诸如ls,dir,fl等)不会清除。如果你非得手工删除别名。请使用

  1. PS C:\PS> del alias:Edit

别名的导入与导出

可以使用Export-Alias将别名导出到文件,需要时再通过Import-Alias导入。但是导入时可能会有异常,提示别名已经存在无法导入:

  1. PS C:\PS> Import-Alias alias.ps1
  2. Import-Alias : Alias not allowed because an alias with the name 'ac' already exists.
  3. At line:1 char:13
  4. + Import-Alias <<<< alias.ps1
  5. + CategoryInfo : ResourceExists: (ac:String) [Import-Alias], SessionStateException
  6. + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand

这时可以使用Force强制导入。

  1. PS C:\PS> Export-Alias alias.ps1
  2. PS C:\PS> Import-Alias -Force alias.ps1

通过函数扩展别名

在Powershell中设置别名的确方便快捷,但是在设置别名的过程中并设置参数的相关信息。尽管别名会自动识别参数,但是如何把经常使用的参数默认设定在别名里面呢?例如Test-Connection -Count 2 -ComputerName,让-“-Count 2” 固化在别名中。
这时简单的别名无法完成上述需求,可以通过函数来完成它,并且一旦把函数拉过来,定义别名会变得更加灵活。

  1. PS C:\PS> function test-conn { Test-Connection -Count 2 -ComputerName $args}
  2. PS C:\PS> Set-Alias tc test-conn
  3. PS C:\PS> tc localhost
  4. Source Destination IPV4Address IPV6Address Bytes Time(ms)
  5. ------ ----------- ----------- ----------- ----- --------
  6. test-me-01 localhost 127.0.0.1 ::1 32 0
  7. test-me-01 localhost 127.0.0.1 ::1 32 0

有了函数牵线,别名可以完成更高级更强大的功能,其中$args为参数的占位符。

永久别名

永久别名配置文件通常位于 ~\Documents\WindowsPowerShell 下的 Microsoft.PowerShell_profile.ps1 文件,可以使用以下命令查看配置文件路径:

  1. PS C:\> $profile

一般该文件在没有创建前是不存在的,使用以下命令为当前用户创建profile命令并返回文件地址:

  1. PS C:\> New-Item -Type file -Force $profile

创建别名可以在此文件中添加:

  1. # touch
  2. Set-Alias touch New-Item
  3. # ls
  4. function getFileName{
  5. Get-ChildItem -Name
  6. }
  7. Remove-Item alias:\ls
  8. Set-Alias ls getFileName
  9. # ll
  10. Set-Alias ll Get-ChildItem
  11. # 编辑
  12. Set-Alias -Name edit -Value notepad
  13. # 打开文件夹
  14. function openFn {
  15. explorer $args
  16. }
  17. Set-Alias open openFn

以后每次在打开PowerShell会话框的时候其会先读取$profile文件中的内容。

参考资料