Author Posts

December 4, 2013 at 4:51 am

I am running a command that's gets all of the .avi files from a path on my computer, i am sorting the output by Length in Descending order and outputting it to a file. What i would like to do is format the returned length property so that is shows in a more manageable format e.g KB/MB/GB. Is there a way to do this?

Get-ChildItem -Path "C:\windows.old\users\user\" -recurse -include *.avi -ea 0 | Sort-Object Length -Descending | format-table -Property Name, Directory, Length | out-file "C:\user_Avi.txt"

Sorry if this is basic i am new to Powershell and still learning/not familiar with nest practices.

Cheers,

Jay

December 4, 2013 at 5:15 am

No worries! Format-Table lets you build constructed properties by passing a hashtable with two keys, "Label" and "Expression" (which can be shortened to L and E, if you prefer). To keep the pipeline readable, I prefer to set up the array to be passed to Format-Table's Property argument ahead of time, for example:

$properties = @(
    'Name'
    'Directory'
    @{
        Label = 'Size'
        Expression = {
            if ($_.Length -ge 1GB)
            {
                '{0:F2} GB' -f ($_.Length / 1GB)
            }
            elseif ($_.Length -ge 1MB)
            {
                '{0:F2} MB' -f ($_.Length / 1MB)
            }
            elseif ($_.Length -ge 1KB)
            {
                '{0:F2} KB' -f ($_.Length / 1KB)
            }
            else
            {
                '{0} bytes' -f $_.Length
            }
        }
    }
)

Get-ChildItem -Path 'C:\windows.old\users\user\' -Recurse -Include *.avi -ErrorAction SilentlyContinue |
Sort-Object -Property Length -Descending |
Format-Table -Property $properties |
Out-File 'C:\user-Avi.txt'

December 4, 2013 at 6:40 am

Thanks very much Dave,

Your explanation was helpful I now get the size column with all of the 'expressions' in MB! I'm not 100% sure with how it works however as like i said i am a beginner. Could you explain this part a little bit further?

if ($_.Length -ge 1GB)
{
'{0:F2} GB' -f ($_.Length / 1GB)
}

What does the -ge and -f do? Also how does '{0:F2} ...' give a the length in a different output? Any extra explanation would be greatly appreciated.

Kind regards,

Jay

December 4, 2013 at 6:53 am

-ge is PowerShell's "greater than or equal to" operator; I set up those conditions to decide whether to display the size in GB, MB, KB, or bytes (anything less than 1KB just displays the bytes, between 1KB and 1MB displays in KB, etc). PowerShell has some convenient numeric literal shortcuts, such as 1GB (which is 1073741824 bytes) so you don't have to do the math yourself.

-f is PowerShell's string format operator. The string on the left is a composite format string (see http://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx for examples of how to build these), and on the right is an argument (or array of arguments) to be formatted inside the string. This command:

'{0:F2} GB' -f ($_.Length / 1GB)

Divides $_.Length by 1GB, leaving you with a decimal number that is the file's size in GB; however, it's likely going to have a large number of digits after the decimal point. By using {0:F2} as the formatting specifier in the string, the value gets rounded to two decimal places for a tidier output. (See http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx for an explanation of the numeric format specifiers available.)

December 4, 2013 at 7:17 am

Thanks for your quick reply again Dave. Great explanation that makes sense to me now. Also, thank you for the links i will have a read through.

Cheers,

Jay