簡短描述
在Windows PowerShell中, 別名就是cmdlets或其他命令的替代名稱.
詳細(xì)描述
別名就是cmdlet或者命令(例如: 函數(shù), 腳本, 文件, 可執(zhí)行文件. )的替代名稱或者說是個(gè)昵稱. 在使用命令的地方, 你都可以使用別名.
cmdlet 的名稱由一個(gè)動(dòng)詞和一個(gè)名詞組成,其功能對(duì)用戶來講一目了然。但是對(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è)別名來源于unix 的shell和windows的cmd。
因此別名有兩個(gè)作用:
繼承:繼承unix-shell和windows-cmd。
方便:方便用戶使用。
處理別名:
查詢別名所指的真實(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
查看可用的別名
查看可用的別名,可以通過” 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:獲取的是別名的數(shù)組,通過where對(duì)數(shù)組元素進(jìn)行遍歷,$_代表當(dāng)前元素,alias的Definition為String類型,因?yàn)閜owershell支持.net,.net中的string類有一個(gè)方法Startswith。通過where過濾集合在powershell中使用非常廣泛。
新聞熱點(diǎn)
疑難解答
圖片精選