Param (
[string]$Path = "N:",
[string]$ReportPath = “N:\Test”,
[switch]$Recurse
)
Function AddObject {
Param (
$FileObject
)
$Size = [double]($FSO.GetFolder($FileObject.FullName).Size)
$Script:TotSize += $Size
# Got rid of the code that converted $Size from a double to a String here
$Script:Report += New-Object PSObject -Property @{
'Folder Name' = $FileObject.FullName
'Created on' = $FileObject.CreationTime
'Last Updated' = $FileObject.LastWriteTime
Size = $Size
Owner = (Get-Acl $FileObject.FullName).Owner
}
}
Function CalculateSize {
Param (
[double]$Size
)
If ($Size -gt 1000000)
{ $ReturnSize = ($Size / 1GB)
}
Else
{ $ReturnSize = ($Size / 1MB)
}
Return $ReturnSize
}
Function Set-AlternatingRows {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[object]$Lines,
[Parameter(Mandatory=$True)]
[string]$CSSEvenClass,
[Parameter(Mandatory=$True)]
[string]$CSSOddClass
)
Begin {
$ClassName = $CSSEvenClass
}
Process {
ForEach ($Line in $Lines)
{ $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
If ($ClassName -eq $CSSEvenClass)
{ $ClassName = $CSSOddClass
}
Else
{ $ClassName = $CSSEvenClass
}
Return $Line
}
}
}
cls
$Report = @()
$TotSize = 0
$FSO = New-Object -ComObject Scripting.FileSystemObject
#First get the properties of the starting path
$Root = Get-Item -Path $Path
AddObject $Root
#Now loop through all the subfolders
$ParamSplat = @{
Path = $Path
Recurse = $Recurse
}
ForEach ($Folder in (Get-ChildItem @ParamSplat | Where { $_.PSisContainer }))
{ AddObject $Folder
}
#Create the HTML for our report
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<Title>
Folder Sizes for “$Path”
</Title>
"@
$TotSize = CalculateSize $TotSize
$Pre = “<h1>Folder Sizes for “”$Path”“</h1><h2>Run on $(Get-Date -f ‘MM/dd/yyyy hh:mm:ss tt’)</h2>”
$Post = “<h2>Total Space Used In “”$($Path)”“: $TotSize</h2></body></html>”
#Create the report and save it to a file
$HTML = $Report | Select ‘Folder Name’,Owner,‘Created On’,‘Last Updated’,Size | Sort ‘Folder Name’ | ConvertTo-Html -PreContent $Pre -PostContent $Post -Head $Header | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | Out-File $ReportPath\FolderSizes.html
$limit = 1GB
$Report | Where-Object { $.Size -gt $limit } | Select-Object “Owner”,@{Name=‘Size’; Expression = { CalculateSize $.Size } } | Export-Csv -Path $ReportPath\FolderSizes.csv -NoTypeInformation
$Report = $Report | Select-object “Folder Name”,“Size”,“Owner”