前置要求

文件名与别名冲突

原因是PS的别名移除命令只能移除当前会话的别名,重写打开PS别名还会存在

  1. 打开PowerShell的配置文件:notepad $profile
  2. 添加移除别名的语句:Remove-Alias ls
  3. 在$profile添加命令函数

    ls

    ```bash function Get-List {

    param (

    1. [bool]$isLs

    )

    $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled) $compressed = New-Object System.Text.RegularExpressions.Regex(‘.(zip|tar|gz|rar|jar|war|7z)$’, $regex_opts) $executable = New-Object System.Text.RegularExpressions.Regex(‘.(exe|bat|cmd|py|ps1|psm1|vbs|rb|reg|sh|zsh)$’, $regex_opts) $code_files = New-Object System.Text.RegularExpressions.Regex(‘.(ini|csv|log|xml|yml|json|java|c|cpp|css|sass|js|ts|jsx|tsx|vue)$’, $regex_opts)

    $itemList = @() Get-ChildItem | ForEach-Object {

    1. $color = ""
    2. $length = $_.Length
    3. if ($_.GetType().Name -eq 'DirectoryInfo') {
    4. $color = "`e[34m" # 目录名称蓝色
    5. $length = ""
    6. }
    7. elseif ($compressed.IsMatch($_.Name)) {
    8. $color = "`e[31m" # 压缩文件红色
    9. }
    10. elseif ($executable.IsMatch($_.Name)) {
    11. $color = "`e[32m" # 可执行文件青色
    12. }
    13. elseif ($code_files.IsMatch($_.Name)) {
    14. $color = "`e[33m" # 代码文件黄色
    15. }
    16. else {
    17. $color = "`e[37m" # 其他文件默认白色
    18. }
    19. $itemList += [PSCustomObject]@{
    20. Mode = $color + $_.Mode
    21. LastWriteTime = $color + $_.LastWriteTime
    22. Length = $color + $length
    23. Name = $color + $_.Name
    24. }

    } if ($isLs) {

    1. Write-Output $itemList | Format-Wide -AutoSize

    } else {

    1. Write-Output $itemList | Format-Table

    } }

function ls { Get-List $true }

  1. <a name="SzMB1"></a>
  2. ### ll
  3. ```bash
  4. function Get-List {
  5. param (
  6. [bool]$isLs
  7. )
  8. $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)
  9. $compressed = New-Object System.Text.RegularExpressions.Regex('\.(zip|tar|gz|rar|jar|war|7z)$', $regex_opts)
  10. $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|py|ps1|psm1|vbs|rb|reg|sh|zsh)$', $regex_opts)
  11. $code_files = New-Object System.Text.RegularExpressions.Regex('\.(ini|csv|log|xml|yml|json|java|c|cpp|css|sass|js|ts|jsx|tsx|vue)$', $regex_opts)
  12. $itemList = @()
  13. Get-ChildItem | ForEach-Object {
  14. $color = ""
  15. $length = $_.Length
  16. if ($_.GetType().Name -eq 'DirectoryInfo') {
  17. $color = "`e[34m" # 目录名称蓝色
  18. $length = ""
  19. }
  20. elseif ($compressed.IsMatch($_.Name)) {
  21. $color = "`e[31m" # 压缩文件红色
  22. }
  23. elseif ($executable.IsMatch($_.Name)) {
  24. $color = "`e[32m" # 可执行文件青色
  25. }
  26. elseif ($code_files.IsMatch($_.Name)) {
  27. $color = "`e[33m" # 代码文件黄色
  28. }
  29. else {
  30. $color = "`e[37m" # 其他文件默认白色
  31. }
  32. $itemList += [PSCustomObject]@{
  33. Mode = $color + $_.Mode
  34. LastWriteTime = $color + $_.LastWriteTime
  35. Length = $color + $length
  36. Name = $color + $_.Name
  37. }
  38. }
  39. if ($isLs) {
  40. Write-Output $itemList | Format-Wide -AutoSize
  41. }
  42. else {
  43. Write-Output $itemList | Format-Table
  44. }
  45. }
  46. function ll {
  47. Get-List $false
  48. }

参考:
https://www.toutiao.com/a7020409939063521822/?log_from=526675665ae6b_1637408373531