Windows Powershell 别名
简短描述
在WindowsPowerShell中,别名就是cmdlets或其他命令的替代名称.
详细描述
别名就是cmdlet或者命令(例如:函数,脚本,文件,可执行文件.)的替代名称或者说是个昵称.在使用命令的地方,你都可以使用别名.
cmdlet的名称由一个动词和一个名词组成,其功能对用户来讲一目了然。但是对于一个经常使用powershell命令的人每天敲那么多命令也很麻烦啊。能不能把命令缩短一点呢?于是“别名”就应运而生了。Powershell内部也实现了很多常用命令的别名。例如Get-ChildItem,列出当前的子文件或目录。它有两个别名:ls和dir,这两个别名来源于unix的shell和windows的cmd。
因此别名有两个作用:
继承:继承unix-shell和windows-cmd。
方便:方便用户使用。
处理别名:
查询别名所指的真实cmdlet命令。
PSC:\PS>Get-Alias-namels CommandTypeNameDefinition ------------------------- AliaslsGet-ChildItem PSC:\PS>Get-Alias-namedir CommandTypeNameDefinition ------------------------- AliasdirGet-ChildItem PSC:\PS>Get-Alias-namefl CommandTypeNameDefinition ------------------------- AliasflFormat-List PSC:\PS>Get-Alias-nameft CommandTypeNameDefinition ------------------------- AliasftFormat-Table
查看可用的别名
查看可用的别名,可以通过”lsalias:”或者”Get-Alias“
如何查看所有以Remove打头的cmdlet的命令的别名呢?
PSC:\PS>diralias:|where{$_.Definition.Startswith("Remove")}
CommandTypeNameDefinition
-------------------------
AliasdelRemove-Item
AliaseraseRemove-Item
AliasrbpRemove-PSBreakpoint
AliasrdRemove-Item
AliasrdrRemove-PSDrive
AliasriRemove-Item
AliasrjbRemove-Job
AliasrmRemove-Item
AliasrmdirRemove-Item
AliasrmoRemove-Module
AliasrpRemove-ItemProperty
AliasrsnRemove-PSSession
AliasrsnpRemove-PSSnapin
AliasrvRemove-Variable
AliasrwmiRemove-WMIObject
说明:diralias:获取的是别名的数组,通过where对数组元素进行遍历,$_代表当前元素,alias的Definition为String类型,因为powershell支持.net,.net中的string类有一个方法Startswith。通过where过滤集合在powershell中使用非常广泛。
有的cmdlet命令可能有2-3个别名,我们可以通过下面的命令查看所有别名和指向cmdlet的别名的个数。
PSC:\PS>lsalias:|Group-Objectdefinition|sort-DescendingCount
CountNameGroup
--------------
6Remove-Item{del,erase,rd,ri...}
3Set-Location{cd,chdir,sl}
3Get-History{ghy,h,history}
3Get-ChildItem{dir,gci,ls}
3Get-Content{cat,gc,type}
3Move-Item{mi,move,mv}
3Copy-Item{copy,cp,cpi}
2Start-Process{saps,start}
2Set-Variable{set,sv}
2Write-Output{echo,write}
2Get-Process{gps,ps}
2Invoke-History{ihy,r}
2New-PSDrive{mount,ndr}
2Stop-Process{kill,spps}
2Rename-Item{ren,rni}
2Get-Location{gl,pwd}
2Compare-Object{compare,diff}
2Where-Object{?,where}
2ForEach-Object{%,foreach}
2Clear-Host{clear,cls}
1Out-Host{oh}
1New-PSSession{nsn}
1New-Variable{nv}
1Out-GridView{ogv}
1Pop-Location{popd}
1Tee-Object{tee}
1Remove-PSBreakpoint{rbp}
1Receive-Job{rcjb}
1Push-Location{pushd}
1mkdir{md}
1Measure-Object{measure}
1help{man}
1Remove-PSSnapin{rsnp}
1Out-Printer{lp}
1New-Item{ni}
1New-Module{nmo}
1New-Alias{nal}
1Move-ItemProperty{mp}
1Wait-Job{wjb}
1Remove-PSDrive{rdr}
1Start-Service{sasv}
1Set-PSBreakpoint{sbp}
1Set-ItemProperty{sp}
1Start-Job{sajb}
1Set-Alias{sal}
1Start-Sleep{sleep}
1Set-Item{si}
1Select-Object{select}
1Set-Content{sc}
1Sort-Object{sort}
1Remove-WMIObject{rwmi}
1Remove-Module{rmo}
1Rename-ItemProperty{rnp}
1Stop-Service{spsv}
1Set-WMIInstance{swmi}
1Remove-Job{rjb}
1Remove-Variable{rv}
1Resolve-Path{rvpa}
1Stop-Job{spjb}
1Remove-ItemProperty{rp}
1Remove-PSSession{rsn}
1Exit-PSSession{exsn}
1Format-Custom{fc}
1Enter-PSSession{etsn}
1Export-Csv{epcsv}
1Export-PSSession{epsn}
1Format-List{fl}
1Get-PSBreakpoint{gbp}
1Get-Command{gcm}
1Get-Alias{gal}
1Format-Table{ft}
1Format-Wide{fw}
1Export-Alias{epal}
1Clear-History{clhy}
1Clear-Item{cli}
1Clear-Content{clc}
1Add-Content{ac}
1Add-PSSnapIn{asnp}
1Clear-ItemProperty{clp}
1Disable-PSBreakpoint{dbp}
1Enable-PSBreakpoint{ebp}
1Convert-Path{cvpa}
1Clear-Variable{clv}
1Copy-ItemProperty{cpp}
1Invoke-Expression{iex}
1Invoke-Item{ii}
1Invoke-Command{icm}
1Get-Variable{gv}
1Get-WmiObject{gwmi}
1Import-Alias{ipal}
1powershell_ise.exe{ise}
1Invoke-WMIMethod{iwmi}
1Import-PSSession{ipsn}
1Import-Csv{ipcsv}
1Import-Module{ipmo}
1Get-Unique{gu}
1Get-Job{gjb}
1Get-Member{gm}
1Get-Item{gi}
1Get-PSCallStack{gcs}
1Get-PSDrive{gdr}
1Get-Module{gmo}
1Get-PSSnapIn{gsnp}
1Get-Service{gsv}
1Get-PSSession{gsn}
1Get-ItemProperty{gp}
1Group-Object{group}
创建自己的别名
给记事本创建一个别名,并查看该别名;
PSC:\PS>Set-Alias-NameEdit-Valuenotepad PSC:\PS>Edit PSC:\PS>$alias:Edit notepad
删除自己的别名
别名不用删除,自定义的别名在powershell退出时会自动清除。但是请放心,powershell内置别名(诸如ls,dir,fl等)不会清除。如果你非得手工删除别名。请使用
PSC:\PS>delalias:Edit保存自己的别名
可以使用Export-Alias将别名导出到文件,需要时再通过Import-Alias导入。但是导入时可能会有异常,提示别名已经存在无法导入:
PSC:\PS>Import-Aliasalias.ps1 Import-Alias:Aliasnotallowedbecauseanaliaswiththename'ac'alreadyexists. Atline:1char:13 +Import-Alias<<<<alias.ps1 +CategoryInfo:ResourceExists:(ac:String)[Import-Alias],SessionStateException +FullyQualifiedErrorId:AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand
这时可以使用Force强制导入。
PSC:\PS>Export-Aliasalias.ps1 PSC:\PS>Import-Alias-Forcealias.ps1
例如,如果你为Get-AuthenticodeSignature设置了别名"gas",你可以直接输入:
gasc:\scripts\sqlscript.ps1
而不必输入:
get-authenticodesignaturec:\scripts\sqlscript.ps1
如果你为微软的Word设置了别名"word",你可以直接输入:
word
而不必输入:
"c:\programfiles\microsoftoffice\office11\winword.exe"
预定义的别名
WindowsPowerShell已经预定义了一部分别名,例如:"cd"和"chdir"都是Set-Location的别名,"ls"和"dir"是Get-Childitem的别名.
查找系统中的所有别名(包括预定别名),输入如下命令:
get-alias
别名相关的CMDLETS
WindowsPowerShell包含了几个cmdlets用于操作别名.
· Get-Alias:取得当前会话(session)中的别名.
· New-Alias:创建一个新的别名.
· Set-Alias:创建或修改一个别名.
· Export-Alias: 导出一个或多个别名到文件中.
· Import-Alias: 导入一个别文件到WindowsPowerShell.
需要cmdlets的详细信息,输入:
get-help<cmdlet-name>-detailed
例如:
get-helpexport-alias-detailed
创建别名
创建一个新的别名,可以使用New-Aliascmdlet.例如,要为Get-Help创建一个"gh"别名,输入,
new-alias-namegh-valueget-help
你可以在命令中就好像你使用的完整的cmdlet名称和各种参数一样,来使用这个别名.
例如,取得Get-WmiObjectcmdlet的详细信息,你只要输入:
get-helpget-wmiobject-detailed
或者
ghget-wmiobject-detailed
保存别名
你创建的别名只在当前的会话(session)有效.要在不同的会话中使用别名,你必须把别名的定义写入你的WindowsPowerShell配置文件,或者使用Export-Alias将别名存储到文件里.
查找别名
要在当前控制台上显示所有别名,包括WindowsPowerShell预定义的别名,你的WindowsPowerShell配置文件中定义的别名,你在当前会话创建的别名,只要输入:
get-alias
如果需要特定的别名,通过为Get-Alias指定Name参数即可.例如,要取得"p"开头的别名,输入
get-alias-namep*
要查找特定cmdlet的所有别名,可以输入:
get-alias|where-object{$_.Definition-eq"<cmdlet-name>"}
例如:
get-alias|where-object{$_.Definition-eq"Remove-Item"}
为带有参数的命令创建别名
你可以为cmdlet,脚本,函数,或者可执行文件赋予别名.但是你不能为带有参数的命令设置别名.例如,你能够为"Get-Eventlog"设置别名,但是你不能为"Get-Eventlog-lognamesecurity"设置别名.
你只能通过创建一个包含该命令的函数来解决这个问题.例如,如下命令创建一个叫做”seclog"的函数,此函数可以表示"get-eventlog-lognamesecurity”命令.
functionseclog{get-eventlog-lognamesecurity}
现在你可以输入用名字"seclog"来简化之前的命令,你还可以为函数"seclog"创建别名.
关于函数的信息,输入:
get-helpabout_function
别名对象
WindowsPowerShell别名实际是类System.Management.Automation.AliasInfo的实例对象.关于对象类型信息,参见MSDN中"AliasInfoClass"的主题.
要查看别名对象上的属性和方法,首先取得别名对象,并且通过管道传递给Get-Membercmdlet.例如,
get-alias|get-member
要查看特定别名的属性值,例如别名"dir",取得该别名并通过管道传递给Format-Listcmdlet.例如,如下代码首先取得别名"dir"对象,通过管道传递给Format-Listcmdlet,通过对Format-List的参数Property赋值为所有(*),来显示别名"dir"的所有属性.
get-alias-namedir|format-list-property*
WINDOWSPOWERSHELL别名PROVIDER
WindowsPowerShell别名provider(译者注:一个Provider就类似于用户使用的文件系统目录结构,微软开发人员通过MVC这种设计思想,将变量,注册表,别名等资源的管理,抽象为文件系统的管理.这样用户可以使用统一的语法对各种资源进行访问.PowerShell开发人员,也能为PowerShell扩展其他的Provider.),使得在WindowsPowerShell中,查看别名就像浏览文件系统驱动器一样.
别名provider提供了"Alias:"驱动器(译者注:虚拟驱动器,只有在PowerShell中有效).要进入Alias:驱动器,输入:
set-locationalias:
要查看该驱动器的内容,输入:
get-childitem
在WindowsPowerShell其他的驱动器时,如果想查看别名驱动器,在目录前要协商驱动器名称,紧跟着一个冒号(:).例如,
get-childitem-pathalias:
要取得特定别名的信息,输入驱动器名称和别名名称,或名称的模式(pattern.笔者注:一般使用的就是通配符.).例如,要取得所有以"p"开头别名的列表,输入:
get-childitem-pathalias:p*
需要更多关于WindowsPowerShell别名provider的信息,输入:
get-helpalias-psprovider
您还可以参考
要列出关于别名的cmdlets,输入:
get-help*-Alias
关于函数的信息,输入:
get-helpabout_function