约 2090 字

约 10 分钟

#PowerShell 的 Get-Alias 命令

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

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

功能

获取当前会话中的别名,或查询指定命令对应的别名。

类型

Cmdlet,属于 Microsoft.PowerShell.Utility。

参数

  • [[-Name] <String[]>] - 要获取的别名名称;支持通配符;位置 0
  • -Definition <String[]> - 获取指定命令的别名;支持通配符
  • -Exclude <String[]> - 排除指定别名;支持通配符
  • -Scope <String> - 指定作用域,可用 Global、Local、Script 或表示父作用域的数字

#示例

列出所有别名

> Get-Alias
CommandType     Name        Version    Source
-----------     ----        -------    ------
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           cd -> Set-Location
Alias           ls -> Get-ChildItem
  • 输出包含别名名称及其指向的命令。

查询指定别名

> Get-Alias -Name ls
> Get-Alias ls, cat
CommandType     Name        Version    Source
-----------     ----        -------    ------
Alias           ls -> Get-ChildItem
Alias           cat -> Get-Content
  • -Name 可省略,支持通配符,例如 Get-Alias -Name g*。

查询命令对应的别名

> Get-Alias -Definition Get-ChildItem
CommandType     Name        Version    Source
-----------     ----        -------    ------
Alias           dir -> Get-ChildItem
Alias           gci -> Get-ChildItem
Alias           ls -> Get-ChildItem
  • 使用 -Definition 可以反查某个命令拥有哪些别名。

排除指定别名

> Get-Alias -Exclude ls, dir, gci
  • -Exclude 用于从结果中排除不需要的别名。

查看指定作用域的别名

> Get-Alias -Scope Global
> Get-Alias -Scope Local
  • 不同作用域可以定义同名别名,-Scope 用于限定查询范围。

#推荐阅读

更新: 2026/11/2

作者: planc

创建: 2026/11/2