evetsleep

Updated Get-FolderSize

Jan 26th, 2015
3,471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #requires -version 2
  2. [CmdletBinding()]
  3. param(
  4.     # Paths to report size, file count, dir count, etc. for.
  5.     [Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Path
  6. )
  7.  
  8. begin{
  9.     # Copyright (c) 2015, Svendsen Tech
  10.     # All rights reserved.
  11.     # Author: Joakim Svendsen
  12.  
  13.     if (-not (Get-Command -Name robocopy -ErrorAction SilentlyContinue)) {
  14.         throw "I need robocopy. Exiting."
  15.     }
  16. }
  17. # PS C:\temp> [datetime]::ParseExact("Mon Jan 26 00:05:19 2015", 'ddd MMM dd HH:mm:ss yyyy', [Globalization.CultureInfo]::InvariantCulture)
  18. process {
  19.     foreach ($p in $Path) {
  20.         Write-Verbose -Message "Processing path: $p. $(Get-Date)"
  21.         if (-not (Test-Path -Path $p -PathType Container)) {
  22.             Write-Warning -Message "$p does not exist or is a file and not a directory. Skipping."
  23.             continue
  24.         }
  25.         $RoboCopyArgs = @("/L","/S","/NJH","/BYTES","/FP","/NC","/NDL","/TS","/XJ","/R:0","/W:0")
  26.         $StartedTime = Get-Date
  27.         [string] $Summary = robocopy $p NULL $RoboCopyArgs | Select-Object -Last 8
  28.         [regex] $HeaderRegex    = '\s+Total\s+Copied\s+Skipped\s+Mismatch\s+FAILED\s+Extras'
  29.         [regex] $DirLineRegex   = 'Dirs\s:\s+(?<DirCount>\d+)(?:\s+\d+){3}\s+(?<DirFailed>\d+)\s+\d+'
  30.         [regex] $FileLineRegex  = 'Files\s:\s+(?<FileCount>\d+)(?:\s+\d+){3}\s+(?<FileFailed>\d+)\s+\d+'
  31.         [regex] $BytesLineRegex = 'Bytes\s:\s+(?<ByteCount>\d+)(?:\s+\d+){3}\s+(?<ByteFailed>\d+)\s+\d+'
  32.         [regex] $TimeLineRegex  = 'Times\s:\s+(?<TimeElapsed>\d+).*'
  33.         [regex] $EndedLineRegex = 'Ended\s:\s+(?<EndedTime>.+)'
  34.         if ($Summary -match "$HeaderRegex\s+$DirLineRegex\s+$FileLineRegex\s+$BytesLineRegex\s+$TimeLineRegex\s+$EndedLineRegex") {
  35.             $ErrorActionPreference = 'Stop'
  36.             try {
  37.                 $EndedTime = [datetime]::ParseExact($Matches['EndedTime'], 'ddd MMM dd HH:mm:ss yyyy', [Globalization.CultureInfo]::InvariantCulture)
  38.             }
  39.             catch {
  40.                 try {
  41.                     $EndedTime = [datetime] $Matches['EndedTime']
  42.                 }
  43.                 catch {
  44.                     $EndedTime = $Matches['EndedTime'] + ' (string)'
  45.                 }
  46.             }
  47.             $ErrorActionPreference = 'Continue'
  48.             New-Object PSObject -Property @{
  49.                 Path        = $p
  50.                 TotalBytes  = [Int64]$Matches['ByteCount']
  51.                 TotalMBytes = [math]::Round(([int64] $Matches['ByteCount'] / 1MB),4)
  52.                 TotalGBytes = [math]::Round(([int64] $Matches['ByteCount'] / 1GB),4)
  53.                 BytesFailed = [Int]$Matches['ByteFailed']
  54.                 DirCount    = [Int]$Matches['DirCount']
  55.                 FileCount   = [Int]$Matches['FileCount']
  56.                 DirFailed   = [Int]$Matches['DirFailed']
  57.                 FileFailed  = [Int]$Matches['FileFailed']
  58.                 StartedTime = $StartedTime
  59.                 EndedTime   = $EndedTime
  60.  
  61.             } | Select Path, TotalBytes, TotalMBytes, TotalGBytes, DirCount, FileCount, DirFailed, FileFailed, StartedTime, EndedTime
  62.         }
  63.         else {
  64.             Write-Warning -Message "$p's output from robocopy was not in an expected format."
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment