使Powershell ls(Get-ChildItem)命令输出易读的大小


ref: Show human-readable file sizes in the default PowerShell ls command

优化?的MyFileFormat.format.ps1xml

<?xml version="1.0" encoding="utf-8" ?>

    
        
            FileSystemTypes
            
                System.IO.DirectoryInfo
                System.IO.FileInfo
            
        
    

    
        
            children
            
                FileSystemTypes
            
            
                PSParentPath
                FileSystemTypes-GroupingFormat
            
            
                
                    
                        
                        7
                        left
                    
                    
                        
                        25
                        right
                    
                    
                        
                        14
                        right
                    
                    
                
                
                    
                        
                        
                            
                                Mode
                            
                            
                                
                                    [String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))
                                
                            
                            
                                FileSize
                            
                            
                                Name
                            
                        
                    
                
            
        
    

优化?的MyTypes.ps1xml

<?xml version="1.0" encoding="utf-8"?>

    
        System.IO.FileInfo
        
            
                
                FileSize
                
                    switch ($this.length) {
                        { $_ -gt 1mb } {
                            if ($_ -lt 1gb)     { "{0:n2} MiB " -f ($_ / 1mb) }
                            elseif ($_ -lt 1tb) { "{0:n2} GiB " -f ($_ / 1gb) }
                            else                { "{0:n2} TiB " -f ($_ / 1tb) }
                            break
                        }
                        default {
                            if ($_ -lt 1kb) { "{0}   B " -f $_ }
                            else            { "{0:n2} KiB " -f ($_ / 1kb) }
                            break
                        }
                    }