簡(jiǎn)短描述
在Windows PowerShell中, 別名就是cmdlets或其他命令的替代名稱(chēng).
詳細(xì)描述
別名就是cmdlet或者命令(例如: 函數(shù), 腳本, 文件, 可執(zhí)行文件. )的替代名稱(chēng)或者說(shuō)是個(gè)昵稱(chēng). 在使用命令的地方, 你都可以使用別名.
cmdlet 的名稱(chēng)由一個(gè)動(dòng)詞和一個(gè)名詞組成,其功能對(duì)用戶(hù)來(lái)講一目了然。但是對(duì)于一個(gè)經(jīng)常使用powershell命令的人每天敲那么多命令也很麻煩啊。能不能把命令縮短一點(diǎn)呢?于是“別名”就應(yīng)運(yùn)而生了。Powershell內(nèi)部也實(shí)現(xiàn)了很多常用命令的別名。例如Get-ChildItem,列出當(dāng)前的子文件或目錄。它有兩個(gè)別名:ls 和 dir,這兩個(gè)別名來(lái)源于unix 的shell和windows的cmd。
因此別名有兩個(gè)作用:
繼承:繼承unix-shell和windows-cmd。
方便:方便用戶(hù)使用。
處理別名:
查詢(xún)別名所指的真實(shí)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
查看可用的別名
查看可用的別名,可以通過(guò)” 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
說(shuō)明:dir alias:獲取的是別名的數(shù)組,通過(guò)where對(duì)數(shù)組元素進(jìn)行遍歷,$_代表當(dāng)前元素,alias的Definition為String類(lèi)型,因?yàn)閜owershell支持.net,.net中的string類(lèi)有一個(gè)方法Startswith。通過(guò)where過(guò)濾集合在powershell中使用非常廣泛。
有的cmdlet命令可能有2-3個(gè)別名,我們可以通過(guò)下面的命令查看所有別名和指向cmdlet的別名的個(gè)數(shù)。
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}
創(chuàng)建自己的別名
給記事本創(chuàng)建一個(gè)別名,并查看該別名;
PS C:/PS> Set-Alias -Name Edit -Value notepadPS C:/PS> EditPS C:/PS> $alias:Editnotepad
刪除自己的別名
別名不用刪除,自定義的別名在powershell退出時(shí)會(huì)自動(dòng)清除。但是請(qǐng)放心,powershell內(nèi)置別名(諸如ls,dir,fl等)不會(huì)清除。如果你非得手工刪除別名。請(qǐng)使用
PS C:/PS> del alias:Edit保存自己的別名
可以使用Export-Alias將別名導(dǎo)出到文件,需要時(shí)再通過(guò)Import-Alias導(dǎo)入。但是導(dǎo)入時(shí)可能會(huì)有異常,提示別名已經(jīng)存在無(wú)法導(dǎo)入:
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
這時(shí)可以使用Force強(qiáng)制導(dǎo)入。
PS C:/PS> Export-Alias alias.ps1PS C:/PS> Import-Alias -Force alias.ps1
例如, 如果你為Get-AuthenticodeSignature設(shè)置了別名"gas", 你可以直接輸入:
gas c:/scripts/sqlscript.ps1
而不必輸入:
get-authenticodesignature c:/scripts/sqlscript.ps1
如果你為微軟的Word設(shè)置了別名"word", 你可以直接輸入:
word
而不必輸入:
"c:/program files/microsoft office/office11/winword.exe"
預(yù)定義的別名
Windows PowerShell已經(jīng)預(yù)定義了一部分別名, 例如: "cd"和"chdir"都是Set-Location的別名, "ls" 和"dir"是Get-Childitem的別名.
查找系統(tǒng)中的所有別名(包括預(yù)定別名), 輸入如下命令:
get-alias
別名相關(guān)的CMDLETS
Windows PowerShell包含了幾個(gè)cmdlets用于操作別名.
? Get-Alias: 取得當(dāng)前會(huì)話(huà)(session)中的別名.
? New-Alias: 創(chuàng)建一個(gè)新的別名.
? Set-Alias: 創(chuàng)建或修改一個(gè)別名.
? Export-Alias: 導(dǎo)出一個(gè)或多個(gè)別名到文件中.
? Import-Alias: 導(dǎo)入一個(gè)別文件到Windows PowerShell.
需要cmdlets的詳細(xì)信息, 輸入:
get-help <cmdlet-name> -detailed
例如:
get-help export-alias -detailed
創(chuàng)建別名
創(chuàng)建一個(gè)新的別名, 可以使用New-Alias cmdlet. 例如, 要為Get-Help創(chuàng)建一個(gè)"gh"別名, 輸入,
new-alias -name gh -value get-help
你可以在命令中就好像你使用的完整的cmdlet名稱(chēng)和各種參數(shù)一樣, 來(lái)使用這個(gè)別名.
例如, 取得Get-WmiObject cmdlet的詳細(xì)信息, 你只要輸入:
get-help get-wmiobject -detailed
或者
gh get-wmiobject -detailed
保存別名
你創(chuàng)建的別名只在當(dāng)前的會(huì)話(huà)(session)有效. 要在不同的會(huì)話(huà)中使用別名, 你必須把別名的定義寫(xiě)入你的Windows PowerShell配置文件, 或者使用Export-Alias將別名存儲(chǔ)到文件里.
查找別名
要在當(dāng)前控制臺(tái)上顯示所有別名, 包括Windows PowerShell預(yù)定義的別名, 你的Windows PowerShell配置文件中定義的別名, 你在當(dāng)前會(huì)話(huà)創(chuàng)建的別名, 只要輸入:
get-alias
如果需要特定的別名, 通過(guò)為Get-Alias指定Name參數(shù)即可. 例如, 要取得"p"開(kāi)頭的別名, 輸入
get-alias -name p*
要查找特定cmdlet的所有別名, 可以輸入:
get-alias | where-object {$_.Definition -eq "<cmdlet-name>"}
例如:
get-alias | where-object {$_.Definition -eq "Remove-Item"}
為帶有參數(shù)的命令創(chuàng)建別名
你可以為cmdlet, 腳本, 函數(shù), 或者可執(zhí)行文件賦予別名. 但是你不能為帶有參數(shù)的命令設(shè)置別名. 例如, 你能夠?yàn)?Get-Eventlog"設(shè)置別名, 但是你不能為"Get-Eventlog -logname security"設(shè)置別名.
你只能通過(guò)創(chuàng)建一個(gè)包含該命令的函數(shù)來(lái)解決這個(gè)問(wèn)題. 例如, 如下命令創(chuàng)建一個(gè)叫做”seclog"的函數(shù), 此函數(shù)可以表示"get-eventlog -logname security”命令.
function seclog {get-eventlog -logname security}
現(xiàn)在你可以輸入用名字"seclog"來(lái)簡(jiǎn)化之前的命令, 你還可以為函數(shù)"seclog"創(chuàng)建別名.
關(guān)于函數(shù)的信息, 輸入:
get-help about_function
別名對(duì)象
Windows PowerShell別名實(shí)際是類(lèi)System.Management.Automation.AliasInfo的實(shí)例對(duì)象. 關(guān)于對(duì)象類(lèi)型信息, 參見(jiàn)MSDN 中"AliasInfo Class"的主題.
要查看別名對(duì)象上的屬性和方法, 首先取得別名對(duì)象, 并且通過(guò)管道傳遞給Get-Member cmdlet. 例如,
get-alias | get-member
要查看特定別名的屬性值, 例如別名"dir", 取得該別名并通過(guò)管道傳遞給Format-List cmdlet. 例如, 如下代碼首先取得別名"dir"對(duì)象, 通過(guò)管道傳遞給Format-List cmdlet, 通過(guò)對(duì)Format-List的參數(shù)Property賦值為所有 (*), 來(lái)顯示別名"dir"的所有屬性.
get-alias -name dir | format-list -property *
WINDOWS POWERSHELL別名PROVIDER
Windows PowerShell別名provider(譯者注: 一個(gè)Provider就類(lèi)似于用戶(hù)使用的文件系統(tǒng)目錄結(jié)構(gòu), 微軟開(kāi)發(fā)人員通過(guò)MVC這種設(shè)計(jì)思想, 將變量, 注冊(cè)表, 別名等資源的管理, 抽象為文件系統(tǒng)的管理. 這樣用戶(hù)可以使用統(tǒng)一的語(yǔ)法對(duì)各種資源進(jìn)行訪(fǎng)問(wèn). PowerShell開(kāi)發(fā)人員, 也能為PowerShell擴(kuò)展其他的Provider.) , 使得在Windows PowerShell中, 查看別名就像瀏覽文件系統(tǒng)驅(qū)動(dòng)器一樣.
別名provider提供了"Alias:"驅(qū)動(dòng)器(譯者注:虛擬驅(qū)動(dòng)器, 只有在PowerShell中有效). 要進(jìn)入Alias: 驅(qū)動(dòng)器, 輸入:
set-location alias:
要查看該驅(qū)動(dòng)器的內(nèi)容, 輸入:
get-childitem
在Windows PowerShell其他的驅(qū)動(dòng)器時(shí), 如果想查看別名驅(qū)動(dòng)器, 在目錄前要協(xié)商驅(qū)動(dòng)器名稱(chēng), 緊跟著一個(gè)冒號(hào)(:). 例如,
get-childitem -path alias:
要取得特定別名的信息, 輸入驅(qū)動(dòng)器名稱(chēng)和別名名稱(chēng), 或名稱(chēng)的模式(pattern. 筆者注: 一般使用的就是通配符. ). 例如, 要取得所有以"p"開(kāi)頭別名的列表, 輸入:
get-childitem -path alias:p*
需要更多關(guān)于Windows PowerShell別名provider的信息, 輸入:
get-help alias-psprovider
您還可以參考
要列出關(guān)于別名的cmdlets, 輸入:
get-help *-Alias
關(guān)于函數(shù)的信息, 輸入:
get-help about_function
新聞熱點(diǎn)
疑難解答
圖片精選