佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Windows Powershell 別名

Windows Powershell 別名

熱門標簽:呼叫中心市場需求 百度競價排名 AI電銷 Linux服務(wù)器 服務(wù)外包 地方門戶網(wǎng)站 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng)

簡短描述
在Windows PowerShell中, 別名就是cmdlets或其他命令的替代名稱.

詳細描述
別名就是cmdlet或者命令(例如: 函數(shù), 腳本, 文件, 可執(zhí)行文件. )的替代名稱或者說是個昵稱. 在使用命令的地方, 你都可以使用別名.

cmdlet 的名稱由一個動詞和一個名詞組成,其功能對用戶來講一目了然。但是對于一個經(jīng)常使用powershell命令的人每天敲那么多命令也很麻煩啊。能不能把命令縮短一點呢?于是“別名”就應(yīng)運而生了。Powershell內(nèi)部也實現(xiàn)了很多常用命令的別名。例如Get-ChildItem,列出當(dāng)前的子文件或目錄。它有兩個別名:ls 和 dir,這兩個別名來源于unix 的shell和windows的cmd。
因此別名有兩個作用:

繼承:繼承unix-shell和windows-cmd。
方便:方便用戶使用。
處理別名:
查詢別名所指的真實cmdlet命令。

PS C:\PS> Get-Alias -name ls

CommandType   Name                        Definition
-----------   ----                        ----------
Alias      ls                         Get-ChildItem

PS C:\PS> Get-Alias -name dir

CommandType   Name                        Definition
-----------   ----                        ----------
Alias      dir                         Get-ChildItem

PS C:\PS> Get-Alias -name fl

CommandType   Name                        Definition
-----------   ----                        ----------
Alias      fl                         Format-List

PS C:\PS> Get-Alias -name ft

CommandType   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-Item
Alias      erase                        Remove-Item
Alias      rbp                         Remove-PSBreakpoint
Alias      rd                         Remove-Item
Alias      rdr                         Remove-PSDrive
Alias      ri                         Remove-Item
Alias      rjb                         Remove-Job
Alias      rm                         Remove-Item
Alias      rmdir                        Remove-Item
Alias      rmo                         Remove-Module
Alias      rp                         Remove-ItemProperty
Alias      rsn                         Remove-PSSession
Alias      rsnp                        Remove-PSSnapin
Alias      rv                         Remove-Variable
Alias      rwmi                        Remove-WMIObject

說明:dir alias:獲取的是別名的數(shù)組,通過where對數(shù)組元素進行遍歷,$_代表當(dāng)前元素,alias的Definition為String類型,因為powershell支持.net,.net中的string類有一個方法Startswith。通過where過濾集合在powershell中使用非常廣泛。

有的cmdlet命令可能有2-3個別名,我們可以通過下面的命令查看所有別名和指向cmdlet的別名的個數(shù)。

PS C:\PS> ls alias: | Group-Object definition | sort -Descending Count

Count 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)建一個別名,并查看該別名;

PS C:\PS> Set-Alias -Name Edit -Value notepad
PS C:\PS> Edit
PS C:\PS> $alias:Edit
notepad

刪除自己的別名
別名不用刪除,自定義的別名在powershell退出時會自動清除。但是請放心,powershell內(nèi)置別名(諸如ls,dir,fl等)不會清除。如果你非得手工刪除別名。請使用

PS C:\PS> del alias:Edit保存自己的別名
可以使用Export-Alias將別名導(dǎo)出到文件,需要時再通過Import-Alias導(dǎo)入。但是導(dǎo)入時可能會有異常,提示別名已經(jīng)存在無法導(dǎo)入:

PS C:\PS> Import-Alias alias.ps1
Import-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強制導(dǎo)入。

PS C:\PS> Export-Alias alias.ps1
PS 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包含了幾個cmdlets用于操作別名.
 
·         Get-Alias: 取得當(dāng)前會話(session)中的別名. 
·         New-Alias: 創(chuàng)建一個新的別名.
·         Set-Alias: 創(chuàng)建或修改一個別名.
·         Export-Alias:  導(dǎo)出一個或多個別名到文件中.
·         Import-Alias:  導(dǎo)入一個別文件到Windows PowerShell.

 

需要cmdlets的詳細信息, 輸入:
 
get-help cmdlet-name> -detailed
 
例如:
 
get-help export-alias -detailed
 
創(chuàng)建別名
創(chuàng)建一個新的別名, 可以使用New-Alias cmdlet. 例如, 要為Get-Help創(chuàng)建一個"gh"別名, 輸入,
 
new-alias -name gh -value get-help
 
你可以在命令中就好像你使用的完整的cmdlet名稱和各種參數(shù)一樣, 來使用這個別名.
 
例如, 取得Get-WmiObject cmdlet的詳細信息, 你只要輸入:
 
get-help get-wmiobject -detailed
 
或者
 
gh get-wmiobject -detailed
 
保存別名
你創(chuàng)建的別名只在當(dāng)前的會話(session)有效. 要在不同的會話中使用別名, 你必須把別名的定義寫入你的Windows PowerShell配置文件, 或者使用Export-Alias將別名存儲到文件里.
 
查找別名
要在當(dāng)前控制臺上顯示所有別名, 包括Windows PowerShell預(yù)定義的別名, 你的Windows PowerShell配置文件中定義的別名, 你在當(dāng)前會話創(chuàng)建的別名, 只要輸入:
 
get-alias
 
如果需要特定的別名, 通過為Get-Alias指定Name參數(shù)即可. 例如, 要取得"p"開頭的別名, 輸入
 
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è)置別名. 例如, 你能夠為"Get-Eventlog"設(shè)置別名, 但是你不能為"Get-Eventlog -logname security"設(shè)置別名.
 
你只能通過創(chuàng)建一個包含該命令的函數(shù)來解決這個問題. 例如, 如下命令創(chuàng)建一個叫做”seclog"的函數(shù), 此函數(shù)可以表示"get-eventlog -logname security”命令.
 
function seclog {get-eventlog -logname security}
 
現(xiàn)在你可以輸入用名字"seclog"來簡化之前的命令, 你還可以為函數(shù)"seclog"創(chuàng)建別名.
 
關(guān)于函數(shù)的信息, 輸入:
 
get-help about_function
 

別名對象

Windows PowerShell別名實際是類System.Management.Automation.AliasInfo的實例對象. 關(guān)于對象類型信息, 參見MSDN 中"AliasInfo Class"的主題.
 
要查看別名對象上的屬性和方法, 首先取得別名對象, 并且通過管道傳遞給Get-Member cmdlet. 例如,
 
get-alias | get-member
 
要查看特定別名的屬性值, 例如別名"dir", 取得該別名并通過管道傳遞給Format-List cmdlet. 例如, 如下代碼首先取得別名"dir"對象, 通過管道傳遞給Format-List cmdlet, 通過對Format-List的參數(shù)Property賦值為所有 (*), 來顯示別名"dir"的所有屬性.
 
get-alias -name dir | format-list -property *
 
WINDOWS POWERSHELL別名PROVIDER

Windows PowerShell別名provider(譯者注: 一個Provider就類似于用戶使用的文件系統(tǒng)目錄結(jié)構(gòu), 微軟開發(fā)人員通過MVC這種設(shè)計思想, 將變量, 注冊表, 別名等資源的管理, 抽象為文件系統(tǒng)的管理. 這樣用戶可以使用統(tǒng)一的語法對各種資源進行訪問. PowerShell開發(fā)人員, 也能為PowerShell擴展其他的Provider.) , 使得在Windows PowerShell中, 查看別名就像瀏覽文件系統(tǒng)驅(qū)動器一樣.
 
別名provider提供了"Alias:"驅(qū)動器(譯者注:虛擬驅(qū)動器, 只有在PowerShell中有效). 要進入Alias: 驅(qū)動器, 輸入:
 
set-location alias:
 
要查看該驅(qū)動器的內(nèi)容, 輸入:
 
get-childitem
 
在Windows PowerShell其他的驅(qū)動器時, 如果想查看別名驅(qū)動器, 在目錄前要協(xié)商驅(qū)動器名稱, 緊跟著一個冒號(:). 例如,
 
get-childitem -path alias:
 
要取得特定別名的信息, 輸入驅(qū)動器名稱和別名名稱, 或名稱的模式(pattern. 筆者注: 一般使用的就是通配符. ). 例如, 要取得所有以"p"開頭別名的列表, 輸入:
 
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

您可能感興趣的文章:
  • Windows Powershell 進行數(shù)學(xué)運算
  • Windows Powershell 執(zhí)行外部命令
  • Windows Powershell 通過函數(shù)擴展別名
  • Windows Powershell 執(zhí)行文件和腳本
  • Windows Powershell 定義變量

標簽:崇左 湖南 湘潭 蘭州 黃山 仙桃 銅川 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Windows Powershell 別名》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    丹东市| 余庆县| 女性| 渝北区| 奇台县| 遵义市| 沁阳市| 乐业县| 新余市| 高台县| 吉林省| 盈江县| 虎林市| 雅安市| 怀安县| 玉环县| 加查县| 车致| 南京市| 婺源县| 汉寿县| 莱西市| 东海县| 郯城县| 东源县| 江达县| 衡南县| 花垣县| 沾化县| 磐石市| 呈贡县| 广宁县| 奉贤区| 石泉县| 河南省| 阿合奇县| 新安县| 固阳县| 望江县| 龙海市| 资中县|