• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Windows Powershell 别名

Windows Powershell 别名

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了windows powershell,windowspowershell2.0,windows powershell64,windows10 powershell,windows7 powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

简短描述
在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 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
</div>

查看可用的别名
查看可用的别名,可以通过” 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
</div>

说明: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 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}
</div>

创建自己的别名
给记事本创建一个别名,并查看该别名;

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

删除自己的别名
别名不用删除,自定义的别名在powershell退出时会自动清除。但是请放心,powershell内置别名(诸如ls,dir,fl等)不会清除。如果你非得手工删除别名。请使用

PS C:\PS> del alias:Edit保存自己的别名
可以使用Export-Alias将别名导出到文件,需要时再通过Import-Alias导入。但是导入时可能会有异常,提示别名已经存在无法导入:

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
</div>

这时可以使用Force强制导入。

PS C:\PS> Export-Alias alias.ps1
PS C:\PS> Import-Alias -Force alias.ps1
</

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • Powershell 获取特定的网页信息的代码
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • 简单谈谈PowerShell 4.0中的新命令

相关文章

  • PowerShell中的变量基础知识介绍
  • PowerShell小技巧之调用CloudFlare的SDK查询网站统计信息
  • PowerShell实现按条件终止管道的方法
  • PowerShell小技巧之实现文件下载(类wget)
  • PowerShell小技巧之尝试ssh登录
  • PowerShell显示隐藏文件和系统文件的方法
  • PowerShell批量修改AD用户密码属性的代码
  • PowerShell编程中的一些命名规则参考
  • PowerShell实现测试端口可用性脚本分享
  • Windows 8 中的 PowerShell 3.0

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell查看Windows功能选项的方法
    • powershell远程管理服务器磁盘空间的实现代码
    • Powershell小技巧之播放WAV声音
    • PowerShell中使用GetType获取变量数据类型
    • 探索PowerShell(十) 循环语句介绍
    • PowerShell Continue语句使用示例
    • Windows Powershell 命令集 cmdlets
    • PowerShell Out-File追加字符串到文件末尾的方法
    • PowerShell把文件删除到回收站的方法
    • PowerShell中定义哈希散列(Hash)和调用例子

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有