make your own suffix like gb, mb, etc

Tagged: 

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of John Steele John Steele 1 week, 5 days ago.

  • Author
    Posts
  • #74920
    Profile photo of js
    js
    Participant

    Is there a way to make your own suffix like gb, mb, etc? Like 10d for 10 days, 10m for 10 minutes, etc.

  • #74924
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yes. PowerShell is open-source; you'd need to fork the repository, contribute that change, and then submit a pull request.

    But other than changing the core code, no.

  • #75065
    Profile photo of John Steele
    John Steele
    Participant

    I've used this Format-FileSize function. Just wrap this function around the number you want to format. I've only found a limited use for it though.

    Function Format-FileSize() {
        Param (
            [parameter(ValueFromPipeline=$true)]
            [int64]$Size
        )
    
        If     ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
        ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
        ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
        ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} KB", $size / 1KB)}
        ElseIf ($size -gt 0)   {[string]::Format("{0:0.00} B", $size)}
        Else                   {""}
    }
    

    I've also seen forum posts where they modify the default view for System.IO.FileInfo objects. A new FileSize property is added that essentially adds a switch statement to change the length property. Take a look at the help files for Update-TypeData.

You must be logged in to reply to this topic.