1

I have a powershell script which get the folder Name,LastWriteTime,Size

cd \myfolder    
get-childitem | where {$_.PSIsContainer} | foreach { 
    $size = ( Get-ChildItem $_ -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum 
    $obj = new-object psobject
    add-member -inp $obj noteproperty path $_.Name
    add-member -inp $obj noteproperty time $_.LastWriteTime
    add-member -inp $obj noteproperty size $size 
    $obj  
    }

Resulting in Path/Name, LastWriteTime, Size

path                                    time                                                                       size
----                                    ----                                                                       ----
Audit_data_network_8-FEB-2017           2/8/2017 10:59:33 AM                                                    1071084
dec-2015                                1/25/2016 10:05:07 AM                                                  29742775
games                                   2/15/2016 11:33:02 AM                                                  52134862
kolachi                                 12/2/2015 3:37:27 PM                                                   12487862
lighroom_                               5/29/2015 2:13:10 PM                                                    2788765
Mini_Remote_Control7.5.9.0_Portable     6/13/2014 3:58:08 PM                                                   52406834
ps                                      2/20/2017 4:23:10 PM                                                     126707
totalcmd                                1/25/2017 4:20:48 PM                                                   11113908

I want to modify the result to add following function

  • Show Size in KB/Mb/GB automatically
  • Sort Result with Size (Most used folder Size on above)

To add auto conversion function, I found following at some forum

# If ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
#ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
#ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
#ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
#ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
#ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" } 
#Write-Output  $sizeOutput  

but I am really stucked from past 2-3 days due to my lack of knowledge in this scripting matter, howto to merge it with existing script and get my desired results ... Any help would be really appreciated ...

0

1 Answer 1

6

You can put the logic in ex. a reusable function and use that to generate the property-value for size. In this solution I used a calculated property instead of a function. That way we can keep the original size (sum of length) for sorting and modify the output after. I also simplified your object-creation. Ex:

Get-FoldersWithSize.ps1:

param ($Path = ".")

$PrettySizeColumn = @{name="Size";expression={
    $size = $_.Size
    if ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
    ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
    ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
    ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
    ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
    ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" } 
    $sizeOutput
}}

Get-ChildItem -Path $Path | Where-Object {$_.PSIsContainer} | ForEach-Object { 
    $size = ( Get-ChildItem -Path $_.FullName -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum 
    $obj = new-object -TypeName psobject -Property @{
        Path = $_.Name
        Time = $_.LastWriteTime
        Size = $size
    }
    $obj  
} | Sort-Object -Property Size -Descending | Select-Object Path, Time, $PrettySizeColumn

Usage:

#Current directory (default value)
.\Get-FoldersWithSize.ps1

#Relative path
.\Get-FoldersWithSize.ps1 -Path ".\Downloads"

#Absolute path
.\Get-FoldersWithSize.ps1 -Path "C:\ProgramData"
6
  • This community is blessing for beginners and numb-nuts like me :) This worked 100% perfect. Many thanks for your time man. Just one more thing. What if I want to provide Folder of my choice , (like we use $1 in script ) / Example: script.ps1 c:\temp (folder of my choice so that I dont have to hardcode it in the script) Feb 22, 2017 at 14:54
  • 1
    Remove the cd \myfolder" part and specify a path for the first Get-ChildItem call. I've updated the answer to work as a script with parameter (last edit...)
    – Frode F.
    Feb 22, 2017 at 15:02
  • got it This worked beautifully :) Salute to all ! Feb 22, 2017 at 15:09
  • The script is now hitting with long path error ` "Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name mu st be less than 248 characters."` The folder I am querying have several hundreds subfolders, with lengthy folder/file name. What are other workarounds to overcome this long path issue ?File server OS is windows 2008 R2 Feb 23, 2017 at 4:17
  • Powershell (actually .NET) doesn't support long paths. People usually opt for robocopy or other solutions. Ex. Map a drive to a share to shorten it etc. Google
    – Frode F.
    Feb 23, 2017 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.