Skip to content

Instantly share code, notes, and snippets.

@pastranastevenaz
Created September 3, 2020 07:44
Show Gist options
  • Select an option

  • Save pastranastevenaz/c860ac0e7ccd6bfeff17ccfc5660c482 to your computer and use it in GitHub Desktop.

Select an option

Save pastranastevenaz/c860ac0e7ccd6bfeff17ccfc5660c482 to your computer and use it in GitHub Desktop.
# Create C:\tmp if it does not already exist
if(!(Test-Path -Path C:\tmp)){
New-Item -ItemType directory -Path C:\tmp *>$null
}
# Create function for getting the size of folders
FUNCTION Get-FolderSize {
BEGIN{$fso = New-Object -ComObject scripting.FileSystemObject}
PROCESS{
$path = $input.fullname
$folder = $fso.GetFolder($path)
$size = $folder.size
[PSCustomObject]@{'Location' = $path; 'GigaBytes' = '{0:f2}' -f ($Size / 1gb)} } }
# Generate report of directories larger than 2GB
$largedirslog = "C:\tmp\Large_Directories-$(get-date -f yyyyMMddHHmm).txt"
Get-ChildItem C:\ -Directory -Recurse -ErrorAction SilentlyContinue -Exclude "C:\inetpub\vhosts\Servers\*\" | Where {$_.FullName -notlike "C:\inetpub\vhosts\Servers\*\" -and $_.FullName -notlike "C:\Windows\*" -and $_.FullName -notlike "C:\Program Files (x86)\*" } | Get-FolderSize | Where {$_.GigaBytes -ge 2} | Sort-Object {$_.GigaBytes -as [int]} -Descending | Format-List * | Out-File $largedirslog -Force
Write-Host "Log of large directories located at $largedirslog"
# Generate report of files larger than 1GB
$largefileslog = "C:\tmp\Large_Files-$(get-date -f yyyyMMddHHmm).txt"
Get-ChildItem C:\ -Recurse -ErrorAction SilentlyContinue -Exclude "C:\inetpub\vhosts\Servers\*" -File | Where-Object {$_.Length -ge 100MB -and $_.FullName -notlike "C:\inetpub\vhosts\Servers\*"} | select FullName, @{Name="MegaBytes";Expression={"{0:f2}" -f ($_.length/1MB)}} | Sort-Object {$_.MegaBytes -as [int]} -Descending | Format-List * | Out-File $largefileslog -Force
Write-Host "Log of large files located in $largefileslog"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment