当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 PowerShell 中获取别名

作者:精品下载站 日期:2024-12-14 16:46:27 浏览:14 分类:玩电脑

在 PowerShell 中获取别名


Get-Alias cmdlet 获取 PowerShell 中当前会话的别名列表。 PowerShell 别名列表包括内置别名、您已添加的别名或您已导入或设置的别名。

Get-Alias 根据别名返回命令名称。如果要返回命令别名列表,请使用定义参数。

在本文中,我们将了解 PowerShell 中的 Get-Alias,以及如何使用 Get-Alias 获取 PowerShell 别名列表或在当前会话中查找别名。

获取别名 PowerShell

Get-Alias 返回 PowerShell cmdlet 的别名。 Get-Alias 将别名作为输入并返回 cmdlet 名称。

默认情况下,Get-Alias 检索在 PowerShell 中为当前会话定义的所有别名。

PowerShell 中 Get-Alias cmdlet 的别名是 gal

语法

Get-Alias
   [[-Name] <String[]>]
   [-Exclude <String[]>]
   [-Scope <String>]
   [<CommonParameters>]

Get-Alias
   [-Exclude <String[]>]
   [-Scope <String>]
   [-Definition <String[]>]
   [<CommonParameters>]

参数

-名称:指定要检索的别名。 名称是一个可选参数。您可以使用通配符。

-定义:获取指定项目的别名。

-Scope:指定 Get-Alias 的有效范围。该范围可以接受全局、本地和脚本。默认情况下,范围是本地

在 PowerShell 中获取当前会话的别名

使用 PowerShell 中的 Get-Alias cmdlet 获取当前会话中的所有别名。

Get-Alias

上述 PowerShell Get-Alias cmdlet 的输出检索当前会话中的所有别名。

PS C:\> 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           clear -> Clear-Host
Alias           clhy -> Clear-History
Alias           cli -> Clear-Item

[玩转系统] 在 PowerShell 中获取别名

酷提示:如何使用 PowerShell Set-Alias cmdlet!

按名称获取所有别名

使用 Get-Alias cmdlet 的 Name 参数获取以字符或名称开头的所有别名。您可以使用通配符。

Get-Alias -Name gc*  

在上面的 PowerShell 脚本中,Get-Alias cmdlet 使用 Name 参数指定带有通配符的别名,以获取以 gc 开头的所有别名。

上述 PowerShell 脚本的输出是:

PS C:\> Get-Alias -Name gc*                                                                                             
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           gc -> Get-Content
Alias           gcb -> Get-Clipboard                               3.1.0.0    Microsoft.PowerShell.Management
Alias           gci -> Get-ChildItem
Alias           gcm -> Get-Command
Alias           gcs -> Get-PSCallStack


PS C:\>   

酷提示:如何为 PowerShell 命令创建新别名!

在 PowerShell 中获取 Cmdlet 别名

您可以使用 Get-AliasDefinition 参数来获取指定 cmdlet 的别名。

 Get-Alias -Definition Get-Command

在上面的 PowerShell 脚本中,Get-Alias 命令使用 Definition 参数指定 cmdlet 名称并检索 PowerShell 中的别名列表。

上述命令的输出是:

PS C:\> Get-Alias -Definition Get-Command                                                                               
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           gcm -> Get-Command


PS C:\>    

酷提示:如何删除 PowerShell 别名!

按属性获取别名

您可以使用 Get-Alias cmdlet 按属性获取所有别名。

Get-Alias | Where-Object {$_.Options -Match "ReadOnly"}

在上面的 PowerShell 脚本中,Get-Alias 命令查找 Options 属性为 ReadOnly 的所有别名。

酷提示:如何使用 PowerShell 获取文件夹中文件的详细信息!

结论

希望上述有关在 PowerShell 中使用 Get-Alias cmdlet 获取别名列表的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯