powershell - 如何在 Powershell 中为命令 "ls -lrt"设置别名

标签 powershell

<分区>

这可能是我缺少的一些简单解决方案。我想为命令行设置别名:

> ls -lrt

要真正做到这一点:

> Get-ChildItem | Sort-Object -Property LastWriteTime

我看到的问题是 ls 已经别名为 Get-Children 并且在我进行任何尝试之前解决了这个问题,例如:

New-Alias -Name 'ls -lrt' -Value 'Get-ChildItem | Sort-Object -Property LastWriteTime'

有谁知道在不损害 ls 以前存在的别名的情况下执行此操作的方法吗?

最佳答案

别名不支持参数。你真正想要的是一个函数:

function ls {
  param(
    [Switch] $lrt
  )
  if ( $lrt ) {
    Get-ChildItem | Sort-Object LastWriteTime
  }
  else {
    Get-ChildItem $args
  }
}

在这种情况下,您需要删除 ls 别名,并改用该函数。

关于powershell - 如何在 Powershell 中为命令 "ls -lrt"设置别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885395/

相关文章:

sharepoint - 完全限定文件名必须少于 260 个字符,但它是

windows - Windows Dockerfile 中的请求下载失败,错误为 'is not recognized'

powershell - powershell:具有可变args的脚本

powershell - 导出模块成员 powershell 别名

powershell - msdeploy密码未知字符

powershell - 通过 powershell 脚本使 ms-edge 窗口全屏显示(通常是 F11)

datetime - 如何在 PowerShell 中格式化日期时间

powershell - Powershell导出为CSV

powershell - PowerShell 中的多行注释

Powershell 返回包含特定文件但不完全递归的目录