Powershell 入门之 Alias

很多时候,我们需要经常运行一些很长的命名,此时,我们可以同给该命令以及部分固定的参数设置一个别名(Alias),这样我们就可以通过运行别名来运行命令,从而简化了命令的输出。

下面我们就来看看 Powershell 的别名吧。

通过 get-alias 获取系统中已经定义的别名:

PS C:\Users\Administrator> Get-Alias

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           asnp -> Add-PSSnapin
Alias           cat -> Get-Content
Alias           cd -> Set-Location
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility
Alias           chdir -> Set-Location
Alias           clc -> Clear-Content
...... ......
...... ......
Alias           spjb -> Stop-Job
Alias           spps -> Stop-Process
Alias           spsv -> Stop-Service
Alias           start -> Start-Process
Alias           stz -> Set-TimeZone                                3.1.0.0    Microsoft.PowerShell.Management
Alias           sujb -> Suspend-Job
Alias           sv -> Set-Variable
Alias           swmi -> Set-WmiInstance
Alias           tee -> Tee-Object
Alias           trcm -> Trace-Command
Alias           type -> Get-Content
Alias           wget -> Invoke-WebRequest
Alias           where -> Where-Object
Alias           wjb -> Wait-Job
Alias           write -> Write-Output

从上面的命令结果,可以看出,Powershell 为了方便与 cmd 一致,在上面建了很多 cmd 命令的别名,这样可以让你在 powershell 中也可以运行 cmd 命令,而不需要重新去习惯使用 powershell 的命令。

我们可以通过 Get-Alias -Definition 加命令,查看命令的是否存在别名:

PS C:\Users\Administrator> Get-Alias -Definition Show-Command

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           shcm -> Show-Command

我们可以通过 get-command 加别名,查看该别名的实际命令:

PS C:\Users\Administrator> Get-Command ft

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ft -> Format-Table

我们可以通过 set-alias 定义我们自己的别名:

PS C:\Users\Administrator> Set-Alias -Name TestPort -Value Test-NetConnection
PS C:\Users\Administrator> Get-Alias TestPort

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           TestPort -> Test-NetConnection

但是,通过上面的方式定义别名后,重启启动 powershell,别名就会失效:

PS C:\Users\Administrator> Get-Alias TestPort
Get-Alias : This command cannot find a matching alias because an alias with the name 'TestPort' does not exist.
At line:1 char:1
+ Get-Alias TestPort
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (TestPort:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

如果想要别名永久生效,你需要将别名写入配置文件。我们可以通过 $profile 查看默认配置文件(默认情况,该文件可能不存在,需要手动创建。):

PS C:\Users\Administrator> $profile
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

创建文件:

PS C:\Users\Administrator> New-Item -Path C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 -ItemType file


    Directory: C:\Users\Administrator\Documents\WindowsPowerShell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        9/15/2021   2:05 AM              0 Microsoft.PowerShell_profile.ps1

文件创建好后,打开该文件,可以用文本编辑器打开,不过,我推荐使用 Powershell ISE 去打开(Powershell ISE 在后面写脚本时,会一直用。),然后把定义别名的命令写入到文件中,并报存:
https://ithelp.ithome.com.tw/upload/images/20210915/20099494zcGflzSmYh.png

此时在重新打开 powershell,就可以正常使用了:

PS C:\Users\Administrator> Get-Alias TestPort

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           TestPort -> Test-NetConnection

<<:  Soundcloud artists can distribute music to other services

>>:  C# 入门之字符串处理

【Day7】 Introduction – If-Else

紧接着,就要开始介绍我们最常用的几种语法啦! 我会分别介绍if-else、while回圈以及for回...

个人笔记 维修单派工 系统架构图

上次的画完系统流程图後,接续开始着手画系统架构图。因之前是资讯管理系毕业,专题中有画过系统架构图,所...

33岁转职者的前端笔记-DAY 12 一些网页切版技巧的小笔记-Part 3

排版小技巧 比较早期的排版都是用表格(table)的方式来排版,现在不会用表格(table)的方式来...

Day 3:安装 Hexo 前置作业:Node.js、Git、网页编辑器 VS code、文章编辑器 Typora

在架设 Hexo 之前,有些前置作业要先进行。其中由於 Hexo 是使用 Node.js 撰写,并且...