Powershell: Properly coloring Get-Childitem output once and for all

后端 未结 6 444
醉梦人生
醉梦人生 2020-12-04 13:14

Edit: Original solution at the bottom of this post. For a more up-to-date solution, see the accepted answer, posted by Thraka.

Colorizing Get-Childit

6条回答
  •  生来不讨喜
    2020-12-04 13:38

    Modifying Out-Default is definitely the way to go. Below a - granted, sloppy - example. I'm using New-CommandWrapper from the PowerShell Cookbook.

    New-CommandWrapper Out-Default `
        -Process {
            if(($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo]))
            {if(-not ($notfirst)) {
               Write-Host "    Directory: $(pwd)`n"           
               Write-Host "Mode                LastWriteTime     Length Name"
               Write-Host "----                -------------     ------ ----"
               $notfirst=$true
               }
               if ($_ -is [System.IO.DirectoryInfo]) {
               Write-host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, ([String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))), $_.length, $_.name) -foregroundcolor "yellow" }
               else {
               Write-host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, ([String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))), $_.length, $_.name) -foregroundcolor "green" }
               $_ = $null
            }
    } 
    

    Example Directory Listing

提交回复
热议问题