Welcome

首页 / 编程脚本 / Windows Powershell 别名

简短描述
在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 NameDefinition----------- --------------Aliasls Get-ChildItemPS C:PS> Get-Alias -name dirCommandType NameDefinition----------- --------------Aliasdir Get-ChildItemPS C:PS> Get-Alias -name flCommandType NameDefinition----------- --------------Aliasfl Format-ListPS C:PS> Get-Alias -name ftCommandType NameDefinition----------- --------------Aliasft Format-Table
查看可用的别名
查看可用的别名,可以通过” ls alias:” 或者 ”Get-Alias“
如何查看所有以Remove打头的cmdlet的命令的别名呢?
PS C:PS> dir alias: | where {$_.Definition.Startswith("Remove")}CommandType NameDefinition----------- --------------Aliasdel Remove-ItemAliaseraseRemove-ItemAliasrbp Remove-PSBreakpointAliasrd Remove-ItemAliasrdr Remove-PSDriveAliasri Remove-ItemAliasrjb Remove-JobAliasrm Remove-ItemAliasrmdirRemove-ItemAliasrmo Remove-ModuleAliasrp Remove-ItemPropertyAliasrsn Remove-PSSessionAliasrsnpRemove-PSSnapinAliasrv Remove-VariableAliasrwmiRemove-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:scriptssqlscript.ps1
 
而不必输入:
 

get-authenticodesignature c:scriptssqlscript.ps1
如果你为微软的Word设置了别名"word", 你可以直接输入:
 
word
 
而不必输入:
 
"c:program filesmicrosoft officeoffice11winword.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