none
How to find total file sizes for files older than 3 months

    Question

  • Hi all

    Is there a command I can run from Powershell that will do the following:

    1. Search a a directory (2TB) for files/folders older than 3 mths
    2. The command will preserve the last accessed date of these files
    3. Will give me the total disk usage the files/folders in step 1 are using up

    So in other words I need a command that will give me the total disk space used by files/folders older than 3 mths.  Is this possible with PS??

    Many thanks in advance!
    Sjmry
    Friday, November 13, 2009 3:54 PM

Answers

  • # two-liner
    $date = (Get-Date).AddMonths(-3)
    dir path\folder -Recurse | ?{$_.lastwritetime -lt $date -and !$_.PsIsContainer} | Measure-Object -Property Length -Sum
    # oneliner
    dir path\folder -Recurse -Force -ErrorAction SilentlyContinue | `
        ?{$_.lastwritetime -lt (Get-Date).AddMonths(-3)} | Measure-Object -Property Length -Sum
    # zero-liner
    # ooops, I haven't zero-liner examples
    #
    # resulting data will be in bytes. To convert them to gigabytes, you may do this:
    $files = dir path\folder -Recurse -Force -ErrorAction SilentlyContinue | `
        ?{$_.lastwritetime -lt (Get-Date).AddMonths(-3)} | Measure-Object -Property Length -Sum
    ($files.sum / 1gb).ToString("F02")
    # F02 determines how much digits will appears after comma. In my case - 2 digits.

    http://www.sysadmins.lv
    • Proposed as answer by Vadims PodansMVP Friday, November 13, 2009 4:33 PM
    • Marked as answer by Wilson Jia Monday, November 16, 2009 10:06 AM
    Friday, November 13, 2009 4:31 PM

All replies

  • # two-liner
    $date = (Get-Date).AddMonths(-3)
    dir path\folder -Recurse | ?{$_.lastwritetime -lt $date -and !$_.PsIsContainer} | Measure-Object -Property Length -Sum
    # oneliner
    dir path\folder -Recurse -Force -ErrorAction SilentlyContinue | `
        ?{$_.lastwritetime -lt (Get-Date).AddMonths(-3)} | Measure-Object -Property Length -Sum
    # zero-liner
    # ooops, I haven't zero-liner examples
    #
    # resulting data will be in bytes. To convert them to gigabytes, you may do this:
    $files = dir path\folder -Recurse -Force -ErrorAction SilentlyContinue | `
        ?{$_.lastwritetime -lt (Get-Date).AddMonths(-3)} | Measure-Object -Property Length -Sum
    ($files.sum / 1gb).ToString("F02")
    # F02 determines how much digits will appears after comma. In my case - 2 digits.

    http://www.sysadmins.lv
    • Proposed as answer by Vadims PodansMVP Friday, November 13, 2009 4:33 PM
    • Marked as answer by Wilson Jia Monday, November 16, 2009 10:06 AM
    Friday, November 13, 2009 4:31 PM
  • Thanks Vadims
    that works really well!  The only issue I have is that I want to be able to tell the size of each individual file and folder also if possible?
    Cheers
    Sjmry
    Monday, November 16, 2009 9:44 AM
  • > folder also if possible

    do you need to include subfolders too? For example top-level folder will show size of itself and all subfolders? Or just each folder size excluding subfoldders?
    http://www.sysadmins.lv
    Monday, November 16, 2009 10:02 AM
  • Just need the individual folder and file sizes so I can pinpoint exact usage
    So C:\temp\test\doc1.txt
    So I would like the output to give the individual sizes of C:\temp, C:\Temp\Test and C:\temp\test\doc1.txt

    Thanks!
    Monday, November 16, 2009 11:48 AM
  • dir -Recurse | ?{$_.psiscontainer} | %{
        Write-Host current folder is $_.fullname;
        dir $_.fullname | measure-object -property Length -sum -ErrorAction SilentlyContinue
    }

    http://www.sysadmins.lv
    Monday, November 16, 2009 12:29 PM