簡短描述
在Windows PowerShell中, 別名就是cmdlets或其他命令的替代名稱.
詳細描述
別名就是cmdlet或者命令(例如: 函數, 指令碼, 檔案, 可執行檔. )的替代名稱或者說是個暱稱. 在使用命令的地方, 你都可以使用別名.
cmdlet 的名稱由一個動詞和一個名片語成,其功能對使用者來講一目瞭然。但是對於一個經常使用powershell命令的人每天敲那麼多命令也很麻煩啊。能不能把命令縮短一點呢?於是“別名”就應運而生了。Powershell內部也實現了很多常用命令的別名。例如Get-ChildItem,列出當前的子檔案或目錄。它有兩個別名:ls 和 dir,這兩個別名來源於unix 的shell和windows的cmd。
因此別名有兩個作用:
繼承:繼承unix-shell和windows-cmd。
方便:方便使用者使用。
處理別名:
查詢別名所指的真實cmdlet命令。
PS C:\PS> Get-Alias -name lsCommandType Name Definition----------- ---- ----------Alias ls Get-ChildItemPS C:\PS> Get-Alias -name dirCommandType Name Definition----------- ---- ----------Alias dir Get-ChildItemPS C:\PS> Get-Alias -name flCommandType Name Definition----------- ---- ----------Alias fl Format-ListPS C:\PS> Get-Alias -name ftCommandType Name Definition----------- ---- ----------Alias ft Format-Table
查看可用的別名
查看可用的別名,可以通過” ls alias:” 或者 ”Get-Alias“
如何查看所有以Remove打頭的cmdlet的命令的別名呢?
PS C:\PS> dir alias: | where {$_.Definition.Startswith("Remove")}CommandType Name Definition----------- ---- ----------Alias del Remove-ItemAlias erase Remove-ItemAlias rbp Remove-PSBreakpointAlias rd Remove-ItemAlias rdr Remove-PSDriveAlias ri Remove-ItemAlias rjb Remove-JobAlias rm Remove-ItemAlias rmdir Remove-ItemAlias rmo Remove-ModuleAlias rp Remove-ItemPropertyAlias rsn Remove-PSSessionAlias rsnp Remove-PSSnapinAlias rv Remove-VariableAlias rwmi Remove-WMIObject
說明:dir alias:擷取的是別名的數組,通過where對數組元素進行遍曆,$_代表當前元素,alias的Definition為String類型,因為powershell支援.net,.net中的string類有一個方法Startswith。通過where過濾集合在powershell中使用非常廣泛。
有的cmdlet命令可能有2-3個別名,我們可以通過下面的命令查看所有別名和指向cmdlet的別名的個數。
PS C:\PS> ls alias: | Group-Object definition | sort -Descending CountCount Name Group----- ---- ----- 6 Remove-Item {del, erase, rd, ri...} 3 Set-Location {cd, chdir, sl} 3 Get-History {ghy, h, history} 3 Get-ChildItem {dir, gci, ls} 3 Get-Content {cat, gc, type} 3 Move-Item {mi, move, mv} 3 Copy-Item {copy, cp, cpi} 2 Start-Process {saps, start} 2 Set-Variable {set, sv} 2 Write-Output {echo, write} 2 Get-Process {gps, ps} 2 Invoke-History {ihy, r} 2 New-PSDrive {mount, ndr} 2 Stop-Process {kill, spps} 2 Rename-Item {ren, rni} 2 Get-Location {gl, pwd} 2 Compare-Object {compare, diff} 2 Where-Object {?, where} 2 ForEach-Object {%, foreach} 2 Clear-Host {clear, cls} 1 Out-Host {oh} 1 New-PSSession {nsn} 1 New-Variable {nv} 1 Out-GridView {ogv} 1 Pop-Location {popd} 1 Tee-Object {tee} 1 Remove-PSBreakpoint {rbp} 1 Receive-Job {rcjb} 1 Push-Location {pushd} 1 mkdir {md} 1 Measure-Object {measure} 1 help {man} 1 Remove-PSSnapin {rsnp} 1 Out-Printer {lp} 1 New-Item {ni} 1 New-Module {nmo} 1 New-Alias {nal} 1 Move-ItemProperty {mp} 1 Wait-Job {wjb} 1 Remove-PSDrive {rdr} 1 Start-Service {sasv} 1 Set-PSBreakpoint {sbp} 1 Set-ItemProperty {sp} 1 Start-Job {sajb} 1 Set-Alias {sal} 1 Start-Sleep {sleep} 1 Set-Item {si} 1 Select-Object {select} 1 Set-Content {sc} 1 Sort-Object {sort} 1 Remove-WMIObject {rwmi} 1 Remove-Module {rmo} 1 Rename-ItemProperty {rnp} 1 Stop-Service {spsv} 1 Set-WMIInstance {swmi} 1 Remove-Job {rjb} 1 Remove-Variable {rv} 1 Resolve-Path {rvpa} 1 Stop-Job {spjb} 1 Remove-ItemProperty {rp} 1 Remove-PSSession {rsn} 1 Exit-PSSession {exsn} 1 Format-Custom {fc} 1 Enter-PSSession {etsn} 1 Export-Csv {epcsv} 1 Export-PSSession {epsn} 1 Format-List {fl} 1 Get-PSBreakpoint {gbp} 1 Get-Command {gcm} 1 Get-Alias {gal} 1 Format-Table {ft} 1 Format-Wide {fw} 1 Export-Alias {epal} 1 Clear-History {clhy} 1 Clear-Item {cli} 1 Clear-Content {clc} 1 Add-Content {ac} 1 Add-PSSnapIn {asnp} 1 Clear-ItemProperty {clp} 1 Disable-PSBreakpoint {dbp} 1 Enable-PSBreakpoint {ebp} 1 Convert-Path {cvpa} 1 Clear-Variable {clv} 1 Copy-ItemProperty {cpp} 1 Invoke-Expression {iex} 1 Invoke-Item {ii} 1 Invoke-Command {icm} 1 Get-Variable {gv} 1 Get-WmiObject {gwmi} 1 Import-Alias {ipal} 1 powershell_ise.exe {ise} 1 Invoke-WMIMethod {iwmi} 1 Import-PSSession {ipsn} 1 Import-Csv {ipcsv} 1 Import-Module {ipmo} 1 Get-Unique {gu} 1 Get-Job {gjb} 1 Get-Member {gm} 1 Get-Item {gi} 1 Get-PSCallStack {gcs} 1 Get-PSDrive {gdr} 1 Get-Module {gmo} 1 Get-PSSnapIn {gsnp} 1 Get-Service {gsv} 1 Get-PSSession {gsn} 1 Get-ItemProperty {gp} 1 Group-Object {group}
建立自己的別名
給記事本建立一個別名,並查看該別名;
PS C:\PS> Set-Alias -Name Edit -Value notepadPS C:\PS> EditPS C:\PS> $alias:Editnotepad
刪除自己的別名
別名不用刪除,自訂的別名在powershell退出時會自動清除。但是請放心,powershell內建別名(諸如ls,dir,fl等)不會清除。如果你非得手工刪除別名。請使用
PS C:\PS> del alias:Edit儲存自己的別名
可以使用Export-Alias將別名匯出到檔案,需要時再通過Import-Alias匯入。但是匯入時可能會有異常,提示別名已經存在無法匯入:
PS C:\PS> Import-Alias alias.ps1Import-Alias : Alias not allowed because an alias with the name 'ac' already exists.At line:1 char:13+ Import-Alias <<<< alias.ps1 + CategoryInfo : ResourceExists: (ac:String) [Import-Alias], SessionStateException + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand
這時可以使用Force強制匯入。
PS C:\PS> Export-Alias alias.ps1PS C:\PS> Import-Alias -Force alias.ps1
例如, 如果你為Get-AuthenticodeSignature設定了別名"gas", 你可以直接輸入:
gas c:\scripts\sqlscript.ps1
而不必輸入:
get-authenticodesignature c:\scripts\sqlscript.ps1
如果你為微軟的Word設定了別名"word", 你可以直接輸入:
word
而不必輸入:
"c:\program files\microsoft office\office11\winword.exe"
預定義的別名
Windows PowerShell已經預定義了一部分別名, 例如: "cd"和"chdir"都是Set-Location的別名, "ls" 和"dir"是Get-Childitem的別名.
尋找系統中的所有別名(包括預定別名), 輸入如下命令:
get-alias
別名相關的CMDLETS
Windows PowerShell包含了幾個cmdlets用於操作別名.
· Get-Alias: 取得當前會話(session)中的別名.
· New-Alias: 建立一個新的別名.
· Set-Alias: 建立或修改一個別名.
· Export-Alias: 匯出一個或多個別名到檔案中.
· Import-Alias: 匯入一個別檔案到Windows PowerShell.
需要cmdlets的詳細資料, 輸入:
get-help <cmdlet-name> -detailed
例如:
get-help export-alias -detailed
建立別名
建立一個新的別名, 可以使用New-Alias cmdlet. 例如, 要為Get-Help建立一個"gh"別名, 輸入,
new-alias -name gh -value get-help
你可以在命令中就好像你使用的完整的cmdlet名稱和各種參數一樣, 來使用這個別名.
例如, 取得Get-WmiObject cmdlet的詳細資料, 你只要輸入:
get-help get-wmiobject -detailed
或者
gh get-wmiobject -detailed
儲存別名
你建立的別名只在當前的會話(session)有效. 要在不同的會話中使用別名, 你必須把別名的定義寫入你的Windows PowerShell設定檔, 或者使用Export-Alias將別名儲存到檔案裡.
尋找別名
要在當前控制台上顯示所有別名, 包括Windows PowerShell預定義的別名, 你的Windows PowerShell設定檔中定義的別名, 你在當前會話建立的別名, 只要輸入:
get-alias
如果需要特定的別名, 通過為Get-Alias指定Name參數即可. 例如, 要取得"p"開頭的別名, 輸入
get-alias -name p*
要尋找特定cmdlet的所有別名, 可以輸入:
get-alias | where-object {$_.Definition -eq "<cmdlet-name>"}
例如:
get-alias | where-object {$_.Definition -eq "Remove-Item"}
為帶有參數的命令建立別名
你可以為cmdlet, 指令碼, 函數, 或者可執行檔賦予別名. 但是你不能為帶有參數的命令設定別名. 例如, 你能夠為"Get-Eventlog"設定別名, 但是你不能為"Get-Eventlog -logname security"設定別名.
你只能通過建立一個包含該命令的函數來解決這個問題. 例如, 如下命令建立一個叫做”seclog"的函數, 此函數可以表示"get-eventlog -logname security”命令.
function seclog {get-eventlog -logname security}
現在你可以輸入用名字"seclog"來簡化之前的命令, 你還可以為函數"seclog"建立別名.
關於函數的資訊, 輸入:
get-help about_function
別名對象
Windows PowerShell別名實際是類System.Management.Automation.AliasInfo的執行個體對象. 關於物件類型資訊, 參見MSDN 中"AliasInfo Class"的主題.
要查看別名對象上的屬性和方法, 首先取得別名對象, 並且通過管道傳遞給Get-Member cmdlet. 例如,
get-alias | get-member
要查看特定別名的屬性值, 例如別名"dir", 取得該別名並通過管道傳遞給Format-List cmdlet. 例如, 如下代碼首先取得別名"dir"對象, 通過管道傳遞給Format-List cmdlet, 通過對Format-List的參數Property賦值為所有 (*), 來顯示別名"dir"的所有屬性.
get-alias -name dir | format-list -property *
WINDOWS POWERSHELL別名PROVIDER
Windows PowerShell別名provider(譯者注: 一個Provider就類似於使用者使用的檔案系統目錄結構, 微軟開發人員通過MVC這種設計思想, 將變數, 註冊表, 別名等資源的管理, 抽象為檔案系統的管理. 這樣使用者可以使用統一的文法對各種資源進行訪問. PowerShell開發人員, 也能為PowerShell擴充其他的Provider.) , 使得在Windows PowerShell中, 查看別名就像瀏覽檔案系統磁碟機一樣.
別名provider提供了"Alias:"磁碟機(譯者注:虛擬磁碟機, 只有在PowerShell中有效). 要進入Alias: 磁碟機, 輸入:
set-location alias:
要查看該磁碟機的內容, 輸入:
get-childitem
在Windows PowerShell其他的磁碟機時, 如果想查看別名磁碟機, 在目錄前要協商磁碟機名稱, 緊跟著一個冒號(:). 例如,
get-childitem -path alias:
要取得特定別名的資訊, 輸入磁碟機名稱和別名名稱, 或名稱的模式(pattern. 筆者注: 一般使用的就是萬用字元. ). 例如, 要取得所有以"p"開頭別名的列表, 輸入:
get-childitem -path alias:p*
需要更多關於Windows PowerShell別名provider的資訊, 輸入:
get-help alias-psprovider
您還可以參考
要列出關於別名的cmdlets, 輸入:
get-help *-Alias
關於函數的資訊, 輸入:
get-help about_function