PowerShell 支持别名。别名允许具有其他 Shell 经验的用户使用其已知的常见命令名称在 PowerShell 中执行类似操作。例如,cmd.exe和Linux shell中的cd命令都是表示改变路径,PowerShell也可以使用cd命令,cd命令是Set-Location命令的别名。

别名功能可帮助用户迅速使用PowerShell。有些Cmdlet有多个别名,Get-ChildItem的别名是ls 和 dir,这两个别名来源于unix 的shell和windows的cmd。

PowerShell不单内置了很多别名,还允许新建自己的别名,下面会一一进行介绍。

查看别名

Get-Alias命令显示与别名相关联的PowerShell命令的真实名称。

Get-Alias cls的结果如下:

1
2
3
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cd -> Set-Location

Get-Alias命令后面不跟任何参数(也就是Get-Alias),则可以查看所有别名。

备注: ls alias:或者 dir alias:也可以查看所有别名

输出的结果如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           asnp -> Add-PSSnapin
Alias           cat -> Get-Content
Alias           cd -> Set-Location
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility
Alias           chdir -> Set-Location
Alias           clc -> Clear-Content
Alias           clear -> Clear-Host
Alias           clhy -> Clear-History
Alias           cli -> Clear-Item
Alias           clp -> Clear-ItemProperty
Alias           cls -> Clear-Host
Alias           clv -> Clear-Variable
Alias           cnsn -> Connect-PSSession
Alias           compare -> Compare-Object
Alias           copy -> Copy-Item
Alias           cp -> Copy-Item
Alias           cpi -> Copy-Item
Alias           cpp -> Copy-ItemProperty
Alias           curl -> Invoke-WebRequest
Alias           cvpa -> Convert-Path
Alias           dbp -> Disable-PSBreakpoint
Alias           del -> Remove-Item
Alias           diff -> Compare-Object
Alias           dir -> Get-ChildItem
Alias           dnsn -> Disconnect-PSSession
Alias           ebp -> Enable-PSBreakpoint
Alias           echo -> Write-Output
Alias           epal -> Export-Alias
Alias           epcsv -> Export-Csv
Alias           epsn -> Export-PSSession
Alias           erase -> Remove-Item
Alias           etsn -> Enter-PSSession
Alias           exsn -> Exit-PSSession
Alias           fc -> Format-Custom
Alias           fhx -> Format-Hex                                  3.1.0.0    Microsoft.PowerShell.Utility
Alias           fl -> Format-List
Alias           foreach -> ForEach-Object
Alias           ft -> Format-Table
Alias           fw -> Format-Wide
Alias           gal -> Get-Alias
Alias           gbp -> Get-PSBreakpoint
Alias           gc -> Get-Content
Alias           gcb -> Get-Clipboard                               3.1.0.0    Microsoft.PowerShell.Management
Alias           gci -> Get-ChildItem
Alias           gcm -> Get-Command
Alias           gcs -> Get-PSCallStack
Alias           gdr -> Get-PSDrive
Alias           ghy -> Get-History
Alias           gi -> Get-Item
Alias           gin -> Get-ComputerInfo                            3.1.0.0    Microsoft.PowerShell.Management
Alias           gjb -> Get-Job
Alias           gl -> Get-Location
Alias           gm -> Get-Member
Alias           gmo -> Get-Module
Alias           gp -> Get-ItemProperty
Alias           gps -> Get-Process
Alias           gpv -> Get-ItemPropertyValue
Alias           group -> Group-Object
Alias           gsn -> Get-PSSession
Alias           gsnp -> Get-PSSnapin
Alias           gsv -> Get-Service
Alias           gtz -> Get-TimeZone                                3.1.0.0    Microsoft.PowerShell.Management
Alias           gu -> Get-Unique
Alias           gv -> Get-Variable
Alias           gwmi -> Get-WmiObject
Alias           h -> Get-History
Alias           history -> Get-History
Alias           icm -> Invoke-Command
Alias           iex -> Invoke-Expression
Alias           ihy -> Invoke-History
Alias           ii -> Invoke-Item
Alias           ipal -> Import-Alias
Alias           ipcsv -> Import-Csv
Alias           ipmo -> Import-Module
Alias           ipsn -> Import-PSSession
Alias           irm -> Invoke-RestMethod
Alias           ise -> powershell_ise.exe
Alias           iwmi -> Invoke-WmiMethod
Alias           iwr -> Invoke-WebRequest
Alias           kill -> Stop-Process
Alias           lp -> Out-Printer
Alias           ls -> Get-ChildItem
Alias           man -> help
Alias           md -> mkdir
Alias           measure -> Measure-Object
Alias           mi -> Move-Item
Alias           mount -> New-PSDrive
Alias           move -> Move-Item
Alias           mp -> Move-ItemProperty
Alias           mv -> Move-Item
Alias           nal -> New-Alias
Alias           ndr -> New-PSDrive
Alias           ni -> New-Item
Alias           nmo -> New-Module
Alias           npssc -> New-PSSessionConfigurationFile
Alias           nsn -> New-PSSession
Alias           nv -> New-Variable
Alias           ogv -> Out-GridView
Alias           oh -> Out-Host
Alias           popd -> Pop-Location
Alias           ps -> Get-Process
Alias           pushd -> Push-Location
Alias           pwd -> Get-Location
Alias           r -> Invoke-History
Alias           rbp -> Remove-PSBreakpoint
Alias           rcjb -> Receive-Job
Alias           rcsn -> Receive-PSSession
Alias           rd -> Remove-Item
Alias           rdr -> Remove-PSDrive
Alias           ren -> Rename-Item
Alias           ri -> Remove-Item
Alias           rjb -> Remove-Job
Alias           rm -> Remove-Item
Alias           rmdir -> Remove-Item
Alias           rmo -> Remove-Module
Alias           rni -> Rename-Item
Alias           rnp -> Rename-ItemProperty
Alias           rp -> Remove-ItemProperty
Alias           rsn -> Remove-PSSession
Alias           rsnp -> Remove-PSSnapin
Alias           rujb -> Resume-Job
Alias           rv -> Remove-Variable
Alias           rvpa -> Resolve-Path
Alias           rwmi -> Remove-WmiObject
Alias           sajb -> Start-Job
Alias           sal -> Set-Alias
Alias           saps -> Start-Process
Alias           sasv -> Start-Service
Alias           sbp -> Set-PSBreakpoint
Alias           sc -> Set-Content
Alias           scb -> Set-Clipboard                               3.1.0.0    Microsoft.PowerShell.Management
Alias           select -> Select-Object
Alias           set -> Set-Variable
Alias           shcm -> Show-Command
Alias           si -> Set-Item
Alias           sl -> Set-Location
Alias           sleep -> Start-Sleep
Alias           sls -> Select-String
Alias           sort -> Sort-Object
Alias           sp -> Set-ItemProperty
Alias           spjb -> Stop-Job
Alias           spps -> Stop-Process
Alias           spsv -> Stop-Service
Alias           start -> Start-Process
Alias           stz -> Set-TimeZone                                3.1.0.0    Microsoft.PowerShell.Management
Alias           sujb -> Suspend-Job
Alias           sv -> Set-Variable
Alias           swmi -> Set-WmiInstance
Alias           tee -> Tee-Object
Alias           trcm -> Trace-Command
Alias           type -> Get-Content
Alias           wget -> Invoke-WebRequest
Alias           where -> Where-Object
Alias           wjb -> Wait-Job
Alias           write -> Write-Output

内置别名的规则

别名不仅仅是为了实现与其他命令 shell 的名称兼容,内置的大多数别名都是为了实现简洁性而设计的。较短的名称更容易键入,但如果你不知道它们所指的是什么,则难以理解。

PowerShell别名尝试兼顾清晰度和简洁性。 PowerShell为常见名词和谓词使用一组标准的别名。 示例缩写:

名词或谓词 缩写
Get g
Set s
Item i
Location l
Command cm
Alias al

了解简写名称后,这些别名是可以理解的。

Cmdlet名称 别名
Get-Item gi
Set-Item si
Get-Location gl
Set-Location sl
Get-Command gcm
Get-Alias gal

创建别名

可以使用Set-Alias cmdlet创建自己的别名。 例如,为Get-Help命令创建别名gh是Set-Alias -Name gh -Value Get-Help

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
PS C:\Users\pwtest> Set-Alias -Name gh -Value Get-Help
PS C:\Users\pwtest> gh

トピック
    Windows PowerShell のヘルプ システム

概要
    Windows PowerShell のコマンドレットと概念に関するヘルプを表示します

詳細説明
    Windows PowerShell ヘルプではWindows PowerShell コマンドレット
    関数スクリプトおよびモジュールとWindows PowerShell 言語の
    要素などの概念について説明します

    Windows PowerShell にヘルプ ファイルが含まれていなくてもオンラインでヘルプ トピックを
    参照できますまたUpdate-Help コマンドレットを使用してヘルプ ファイルを
    コンピューターにダウンロードしGet-Help コマンドレットを使用してコマンド ラインで
    ヘルプ トピックを表示できます

    Update-Help コマンドレットを使用すると更新されたヘルプ ファイルをリリース時に
    ダウンロードしてローカルのヘルプ コンテンツが古くならないようにすることもできます

    ヘルプ ファイルがない場合はGet-Help を実行するとコマンドレット関数
    およびスクリプトについて自動生成されたヘルプが表示されます


  オンライン ヘルプ
    Windows PowerShell のオンライン ヘルプはTechNet ライブラリにあります
    まずはhttp://go.microsoft.com/fwlink/?LinkID=108518 を参照してください

    任意のコマンドレッドまたは関数のオンライン ヘルプを表示するには次のように入力します

        Get-Help <コマンドレット名> -Online

  UPDATE-HELP
    コンピューターにヘルプ ファイルをダウンロードしてインストールするには次の手順を実行します

       1.[管理者として実行] オプションを使用して Windows PowerShell を起動します
       2.次のように入力します

          Update-Help

    ヘルプ ファイルがインストールされたらGet-Help コマンドレットを使用して
    ヘルプ トピックを表示できますまたUpdate-Help コマンドレットを使用すると
    ローカルのヘルプ ファイルが常に最新の状態で保たれるように更新された
    ヘルプ ファイルをダウンロードできます

    Update-Help コマンドレットの詳細を確認するには次のように入力します

       Get-Help Update-Help -Online

    またはhttp://go.microsoft.com/fwlink/?LinkID=210614 を参照してください


  GET-HELP
    Get-Help コマンドレットを実行するとコンピューターに保存されているヘルプ ファイルのコンテンツから
    コマンド ラインにヘルプが表示されますヘルプ ファイルがない場合に Get-Help を実行すると
    コマンドレットと関数について基本的なヘルプが表示されますまたGet-Help を使用して
    コマンドレットと関数についてオンライン ヘルプを表示することもできます

    コマンドレットのヘルプを表示するには次のように入力します

        Get-Help <コマンドレット名>

    オンライン ヘルプを表示するには次のように入力します

        Get-Help <コマンドレット名> -Online

    概念説明のトピックのタイトルは "About_" で始まります
    概念または言語要素のヘルプを表示するには次のように入力します

        Get-Help About_<トピック名>

    すべてのヘルプ ファイルで単語や語句を検索するには次のように入力します

        Get-Help <検索用語>

    Get-Help コマンドレットの詳細を確認するには次のように入力します

        Get-Help Get-Help -Online

    またはhttp://go.microsoft.com/fwlink/?LinkID=113316 を参照してください


  :
      Save-Help              : インターネットからヘルプ ファイルをダウンロードして
                               ファイル共有に保存します
      Update-Help            : インターネットまたはファイル共有からヘルプ ファイルを
                               ダウンロードしてインストールします
      Get-Help Get-Process   : Get-Process コマンドレットに関するヘルプを表示します
      Get-Help Get-Process -Online
                             : Get-Process コマンドレットのオンライン ヘルプを開きます
      Help Get-Process       : Get-Process に関するヘルプを 1 ページずつ表示します
      Get-Process -?         : Get-Process コマンドレットに関するヘルプを表示します
      Get-Help About_Modules : Windows PowerShell モジュールに関するヘルプを表示します
      Get-Help remoting      : ヘルプ トピックで "リモート処理" という単語を検索します

  関連項目:
      about_Updatable_Help
      Get-Help
      Save-Help
      Update-Help

前面介绍的内置的别名有一部分是不可以更改的,如果尝试更改,你将收到一个错误,该错误说明别名无法进行修改。因为这些内置的别名是只读的,不许更改。例如,内置别名gi是只读的,不可以更改。

1
2
3
4
PS> Set-Alias -Name gi -Value Get-Item
Set-Alias : Alias is not writeable because alias gi is read-only or constant and cannot be written to.
At line:1 char:10
+ Set-Alias  <<<< -Name gi -Value Get-Item

内置别名cat是可读写的,可以更改。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
PS C:\Users\pwtest> Get-Alias cat

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cat -> Get-Content

PS C:\Users\pwtest> Set-Alias -name ca -value Get-Content
PS C:\Users\pwtest> ca C:\Users\pwtest\Desktop\temp\hello.txt
hello content.
PS C:\Users\pwtest>

删除别名

别名不用删除,自定义的别名在powershell退出时会自动清除。powershell内置别名(诸如ls,dir,fl等)不会清除。如果你非得手工删除别名。请使用del Alias:别名。例如删除自定义get-help的别名gh,del Alias:gh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
PS C:\Users\pwtest> del alias:gh
PS C:\Users\pwtest> gh
gh : 用語 'gh' コマンドレット関数スクリプト ファイルまたは操作可能なプログラムの名前として認識されません
前が正しく記述されていることを確認しパスが含まれている場合はそのパスが正しいことを確認してから再試行してください
発生場所 :1 文字:1
+ gh
+ ~~
    + CategoryInfo          : ObjectNotFound: (gh:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\pwtest>

导出别名到文件

可以使用Export-Alias将别名导出到文件,需要时再通过Import-Alias导入。默认情况下,Import-Alias会把内置和自定义的别名输出到文件。例如,输出所有的别名到alias.ps1文件,并利用别名命令ca查看文件内容。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
PS C:\Users\pwtest> Export-Alias C:\Users\pwtest\Desktop\temp\alias.ps1
PS C:\Users\pwtest> ca C:\Users\pwtest\Desktop\temp\alias.ps1
# エイリアス ファイル
# エクスポート ユーザー: pwtest
# 日付/時刻: 2019年9月26日 15:41:50
# コンピューター: pwtest-PC
"foreach","ForEach-Object","","ReadOnly, AllScope"
"%","ForEach-Object","","ReadOnly, AllScope"
"where","Where-Object","","ReadOnly, AllScope"
"?","Where-Object","","ReadOnly, AllScope"
"ac","Add-Content","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
"cli","Clear-Item","","ReadOnly, AllScope"
"clp","Clear-ItemProperty","","ReadOnly, AllScope"
"clv","Clear-Variable","","ReadOnly, AllScope"
"compare","Compare-Object","","ReadOnly, AllScope"
"cpi","Copy-Item","","ReadOnly, AllScope"
"cpp","Copy-ItemProperty","","ReadOnly, AllScope"
"cvpa","Convert-Path","","ReadOnly, AllScope"
"dbp","Disable-PSBreakpoint","","ReadOnly, AllScope"
"diff","Compare-Object","","ReadOnly, AllScope"
"ebp","Enable-PSBreakpoint","","ReadOnly, AllScope"
"epal","Export-Alias","","ReadOnly, AllScope"
"epcsv","Export-Csv","","ReadOnly, AllScope"
"fc","Format-Custom","","ReadOnly, AllScope"
"fl","Format-List","","ReadOnly, AllScope"
"ft","Format-Table","","ReadOnly, AllScope"
"fw","Format-Wide","","ReadOnly, AllScope"
"gal","Get-Alias","","ReadOnly, AllScope"
"gbp","Get-PSBreakpoint","","ReadOnly, AllScope"
"gc","Get-Content","","ReadOnly, AllScope"
"gci","Get-ChildItem","","ReadOnly, AllScope"
"gcm","Get-Command","","ReadOnly, AllScope"
"gdr","Get-PSDrive","","ReadOnly, AllScope"
"gcs","Get-PSCallStack","","ReadOnly, AllScope"
"ghy","Get-History","","ReadOnly, AllScope"
"gi","Get-Item","","ReadOnly, AllScope"
"gl","Get-Location","","ReadOnly, AllScope"
"gm","Get-Member","","ReadOnly, AllScope"
"gmo","Get-Module","","ReadOnly, AllScope"
"gp","Get-ItemProperty","","ReadOnly, AllScope"
"gpv","Get-ItemPropertyValue","","ReadOnly, AllScope"
"gps","Get-Process","","ReadOnly, AllScope"
"group","Group-Object","","ReadOnly, AllScope"
"gsv","Get-Service","","ReadOnly, AllScope"
"gu","Get-Unique","","ReadOnly, AllScope"
"gv","Get-Variable","","ReadOnly, AllScope"
"iex","Invoke-Expression","","ReadOnly, AllScope"
"ihy","Invoke-History","","ReadOnly, AllScope"
"ii","Invoke-Item","","ReadOnly, AllScope"
"ipmo","Import-Module","","ReadOnly, AllScope"
"ipal","Import-Alias","","ReadOnly, AllScope"
"ipcsv","Import-Csv","","ReadOnly, AllScope"
"measure","Measure-Object","","ReadOnly, AllScope"
"mi","Move-Item","","ReadOnly, AllScope"
"mp","Move-ItemProperty","","ReadOnly, AllScope"
"nal","New-Alias","","ReadOnly, AllScope"
"ndr","New-PSDrive","","ReadOnly, AllScope"
"ni","New-Item","","ReadOnly, AllScope"
"nv","New-Variable","","ReadOnly, AllScope"
"nmo","New-Module","","ReadOnly, AllScope"
"oh","Out-Host","","ReadOnly, AllScope"
"rbp","Remove-PSBreakpoint","","ReadOnly, AllScope"
"rdr","Remove-PSDrive","","ReadOnly, AllScope"
"ri","Remove-Item","","ReadOnly, AllScope"
"rni","Rename-Item","","ReadOnly, AllScope"
"rnp","Rename-ItemProperty","","ReadOnly, AllScope"
"rp","Remove-ItemProperty","","ReadOnly, AllScope"
"rmo","Remove-Module","","ReadOnly, AllScope"
"rv","Remove-Variable","","ReadOnly, AllScope"
"rvpa","Resolve-Path","","ReadOnly, AllScope"
"sal","Set-Alias","","ReadOnly, AllScope"
"sasv","Start-Service","","ReadOnly, AllScope"
"sbp","Set-PSBreakpoint","","ReadOnly, AllScope"
"sc","Set-Content","","ReadOnly, AllScope"
"select","Select-Object","","ReadOnly, AllScope"
"si","Set-Item","","ReadOnly, AllScope"
"sl","Set-Location","","ReadOnly, AllScope"
"sleep","Start-Sleep","","ReadOnly, AllScope"
"sort","Sort-Object","","ReadOnly, AllScope"
"sp","Set-ItemProperty","","ReadOnly, AllScope"
"saps","Start-Process","","ReadOnly, AllScope"
"start","Start-Process","","ReadOnly, AllScope"
"spps","Stop-Process","","ReadOnly, AllScope"
"spsv","Stop-Service","","ReadOnly, AllScope"
"sv","Set-Variable","","ReadOnly, AllScope"
"tee","Tee-Object","","ReadOnly, AllScope"
"write","Write-Output","","ReadOnly, AllScope"
"irm","Invoke-RestMethod","","ReadOnly, AllScope"
"iwr","Invoke-WebRequest","","ReadOnly, AllScope"
"asnp","Add-PSSnapIn","","ReadOnly, AllScope"
"gsnp","Get-PSSnapIn","","ReadOnly, AllScope"
"gwmi","Get-WmiObject","","ReadOnly, AllScope"
"iwmi","Invoke-WMIMethod","","ReadOnly, AllScope"
"ogv","Out-GridView","","ReadOnly, AllScope"
"ise","powershell_ise.exe","","ReadOnly, AllScope"
"rsnp","Remove-PSSnapin","","ReadOnly, AllScope"
"rwmi","Remove-WMIObject","","ReadOnly, AllScope"
"swmi","Set-WMIInstance","","ReadOnly, AllScope"
"shcm","Show-Command","","ReadOnly, AllScope"
"trcm","Trace-Command","","ReadOnly, AllScope"
"wget","Invoke-WebRequest","","AllScope"
"curl","Invoke-WebRequest","","AllScope"
"lp","Out-Printer","","AllScope"
"cat","Get-Content","","AllScope"
"cd","Set-Location","","AllScope"
"clear","Clear-Host","","AllScope"
"cp","Copy-Item","","AllScope"
"h","Get-History","","AllScope"
"history","Get-History","","AllScope"
"kill","Stop-Process","","AllScope"
"ls","Get-ChildItem","","AllScope"
"man","help","","AllScope"
"mount","New-PSDrive","","AllScope"
"md","mkdir","","AllScope"
"mv","Move-Item","","AllScope"
"popd","Pop-Location","","AllScope"
"ps","Get-Process","","AllScope"
"pushd","Push-Location","","AllScope"
"pwd","Get-Location","","AllScope"
"r","Invoke-History","","AllScope"
"rm","Remove-Item","","AllScope"
"rmdir","Remove-Item","","AllScope"
"echo","Write-Output","","AllScope"
"cls","Clear-Host","","AllScope"
"chdir","Set-Location","","AllScope"
"copy","Copy-Item","","AllScope"
"del","Remove-Item","","AllScope"
"dir","Get-ChildItem","","AllScope"
"erase","Remove-Item","","AllScope"
"move","Move-Item","","AllScope"
"rd","Remove-Item","","AllScope"
"ren","Rename-Item","","AllScope"
"set","Set-Variable","","AllScope"
"type","Get-Content","","AllScope"
"icm","Invoke-Command","","AllScope"
"clhy","Clear-History","","ReadOnly, AllScope"
"gjb","Get-Job","","AllScope"
"rcjb","Receive-Job","","AllScope"
"rjb","Remove-Job","","AllScope"
"sajb","Start-Job","","AllScope"
"spjb","Stop-Job","","AllScope"
"wjb","Wait-Job","","AllScope"
"sujb","Suspend-Job","","AllScope"
"rujb","Resume-Job","","AllScope"
"npssc","New-PSSessionConfigurationFile","","ReadOnly, AllScope"
"ipsn","Import-PSSession","","AllScope"
"epsn","Export-PSSession","","AllScope"
"cnsn","Connect-PSSession","","ReadOnly, AllScope"
"dnsn","Disconnect-PSSession","","ReadOnly, AllScope"
"nsn","New-PSSession","","AllScope"
"gsn","Get-PSSession","","AllScope"
"rsn","Remove-PSSession","","AllScope"
"etsn","Enter-PSSession","","AllScope"
"rcsn","Receive-PSSession","","ReadOnly, AllScope"
"exsn","Exit-PSSession","","AllScope"
"sls","Select-String","","None"
"CFS","ConvertFrom-String","","None"
"fhx","Format-Hex","","None"
"gcb","Get-Clipboard","","None"
"gin","Get-ComputerInfo","","None"
"gtz","Get-TimeZone","","None"
"scb","Set-Clipboard","","None"
"stz","Set-TimeZone","","None"
"ca","Get-Content","","None"
PS C:\Users\pwtest>

导入别名文件

既然能导出别名文件,那么肯定能导入别名文件。通过Import-Alias命令来导入别名文件。但是导入时可能会有异常,提示别名已经存在无法导入,这时候需要添加-force参数,强制导入就可以了。

例如,新开启一个PowerShell命令行界面,这时没有别名ca。导入刚才导出的别名文件alias.ps1后,可以使用ca。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PS C:\Users\pwtest> ca
ca : 用語 'ca' コマンドレット関数スクリプト ファイルまたは操作可能なプログラムの名前として認識されません
前が正しく記述されていることを確認しパスが含まれている場合はそのパスが正しいことを確認してから再試行してください
発生場所 :1 文字:1
+ ca
+ ~~
    + CategoryInfo          : ObjectNotFound: (ca:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\pwtest> Import-Alias C:\Users\pwtest\Desktop\temp\alias.ps1
Import-Alias : 名前 'foreach' のエイリアスが既に存在するためこのエイリアスは使用できません
発生場所 :1 文字:1
+ Import-Alias C:\Users\pwtest\Desktop\temp\alias.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (foreach:String) [Import-Alias], SessionStateException
    + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand
……
……

PS C:\Users\pwtest> Import-Alias -Force C:\Users\pwtest\Desktop\temp\alias.ps1
PS C:\Users\pwtest> ca C:\Users\pwtest\Desktop\temp\hello.txt
hello content.
PS C:\Users\pwtest>

参考资料:
https://docs.microsoft.com/zh-cn/powershell/scripting/learn/using-familiar-command-names?view=powershell-7

转载请注明本网址。