简短说明
介绍如何在 PowerShell 中使用 cmdlet 和命令的备用名称。
详细说明
别名是 cmdlet 或命令元素(例如函数、脚本、文件或可执行文件)的另一名称或昵称。 可以在任何 PowerShell 命令中使用别名而不是命令名称。
若要创建别名,请使用 New-Alias
cmdlet。 例如,以下命令为 gas
cmdlet 创建 Get-AuthenticodeSignature
别名:
New-Alias -Name gas -Value Get-AuthenticodeSignature
创建 cmdlet 名称的别名后,您可以使用该别名代替 cmdlet 名称。 例如,若要获取 Authenticode 文件的 SqlScript.ps1
签名,请键入:
Get-AuthenticodeSignature SqlScript.ps1
或者,键入:
gas SqlScript.ps1
If you create word
as the alias for Microsoft Office Word, you can type "word" instead of the following:
"C:\Program Files\Microsoft Office\Office11\Winword.exe"
Built-in aliases
PowerShell 包括一组内置别名,包括 cd
cmdlet 的 chdir
和 Set-Location
、Windows 上的 ls
和 dir
,以及适用于 dir
cmdlet 的 Linux 和 macOS 上的 Get-ChildItem
。
若要获取计算机上的所有别名,包括内置别名,请键入:
Get-Alias
Alias cmdlet
PowerShell 包括以下 cmdlet(命令),这些命令专为处理别名而设计:
-
Get-Alias
- 获取当前会话中的所有别名。 -
New-Alias
- 创建新的别名。 -
Set-Alias
- 创建或更改别名。 -
Remove-Alias
- 删除别名。 -
Export-Alias
- Exports one or more aliases to a file. -
Import-Alias
- 将别名文件导入 PowerShell。
For detailed information about the cmdlets, type:
Get-Help <cmdlet-Name> -Detailed
例如,键入:
Get-Help Export-Alias -Detailed
创建别名
若要创建新的别名,请使用 New-Alias
cmdlet。 例如,若要为 gh
创建 Get-Help
别名,请键入:
New-Alias -Name gh -Value Get-Help
可以使用命令中的别名,就像使用完整的 cmdlet 名称一样,还可以将别名与参数一起使用。
例如,若要获取 Get-CimInstance
cmdlet 的详细帮助,请键入:
Get-Help Get-CimInstance -Detailed
或者,键入:
gh Get-CimInstance -Detailed
保存别名
创建的别名仅保存在当前会话中。 若要在不同的会话中使用别名,请将别名添加到 PowerShell 配置文件。 或者,使用 Export-Alias
cmdlet 将别名保存到文件中。
For more information, type:
Get-Help about_Profiles
Getting aliases
若要获取当前会话中的所有别名,包括内置别名、PowerShell 配置文件中的别名以及你在当前会话中创建的别名,请键入:
Get-Alias
若要获取特定别名,请使用 Get-Alias
cmdlet 的 Name 参数。 例如,若要获取以“p”开头的别名,请键入:
Get-Alias -Name p*
若要获取特定项的别名,请使用 Definition 参数。 For example, to get the aliases for the Get-ChildItem
cmdlet type:
Get-Alias -Definition Get-ChildItem
Get-Alias output
Get-Alias
只返回一种类型的对象,AliasInfo 对象(System.Management.Automation.AliasInfo)。 不包含连字符的别名的名称(如 cd
)以以下格式显示:
Get-Alias ac
CommandType Name Version Source
----------- ---- ------- ------
Alias ac -> Add-Content
这使得获取所需信息变得非常快速且容易。
基于箭头的别名格式不用于包含连字符的别名。 这些名称可能是 cmdlet 和函数的更合适的替代名称,而不是常见的缩写或昵称,作者可能不希望它们那么明显。
具有参数的命令的备用名称
可以将别名分配给 cmdlet、脚本、函数或可执行文件。 不能为命令及其参数分配别名。 例如,可以将别名分配给 Get-Eventlog
cmdlet,但不能将别名分配给 Get-Eventlog -LogName System
命令。
可以创建包含命令的函数。 若要创建函数,请键入单词“function”,后跟函数的名称。 键入命令,并将其括在大括号({})中。
例如,以下命令创建 syslog 函数。 此函数表示 Get-Eventlog -LogName System
命令:
function Get-SystemEventlog {Get-Eventlog -LogName System}
Set-Alias -Name syslog -Value Get-SystemEventlog
现在可以键入“syslog”而不是命令。 还可以为新函数创建别名。
For more information about functions, type:
Get-Help about_Functions
Alias objects
PowerShell 别名由属于 System.Management.Automation.AliasInfo 类实例的对象表示。 有关此类型的对象的详细信息,请参阅 PowerShell SDK 中的 AliasInfo 类。
若要查看别名对象的属性和方法,请获取别名。
Then, pipe them to the Get-Member
cmdlet. 例如:
Get-Alias | Get-Member
若要查看特定别名(如 dir
别名)的属性的值,请获取该别名。 Then, pipe it to the Format-List
cmdlet. 例如,以下命令获取 dir
别名。 Next, the command pipes the alias to the Format-List
cmdlet. 然后,该命令使用具有通配符(Format-List
)的 *
属性参数来显示 dir
别名的所有属性。 以下命令执行以下任务:
Get-Alias -Name dir | Format-List -Property *
PowerShell Alias provider
PowerShell includes the Alias provider. Alias 提供程序允许你在 PowerShell 中查看别名,就像它们位于文件系统驱动器上一样。
The Alias provider exposes the Alias: drive. To go into the Alias: drive, type:
Set-Location Alias:
若要查看驱动器的内容,请键入:
Get-ChildItem
如果要从另一个 PowerShell 驱动器查看某个驱动器的内容,请在路径开头填写该驱动器的名称。 Include the colon (:). 例如:
Get-ChildItem -Path Alias:
若要获取有关特定别名的信息,请键入驱动器名称和别名。 Or, type a name pattern. 例如,若要获取以“p”开头的所有别名,请键入:
Get-ChildItem -Path Alias:p*
要获取有关 PowerShell Alias 提供程序的更多信息,请在命令行中输入:
Get-Help Alias