Get IIS websites size

asked 2021-11-06T09:30:58.7+00:00
Mr Edge 121 Reputation points

Im trying to get a list of sites from IIS (8.5) to include the folder size but cant get this to work.

Below is the current code i have that is working without the size

Import-Module webAdministration # Required for Powershell v2 or lower  

$filepath = C:\sites.csv
$sites = get-website

foreach($site in $sites) {
    $name = $site.name
    $bindings = $site.bindings.collection.bindinginformation.split(":")
    $ip = $bindings[0]
    $port = $bindings[1]
    $hostHeader = $bindings[2]
    "$name,$hostHeader,$ip,$port" | out-host
    "$name,$hostHeader,$ip,$port" | out-file $filePath -Append
}

I then attempted to add in this line

$size = Get-ChildItem -Directory -Force|ForEach {"{0,-30} {1,-30} {2:N2}MB" -f $_.Name, $_.LastWriteTime, ((Get-ChildItem $_ -Recurse|Measure-Object -Property Length -Sum -ErrorAction Stop).Sum/1MB)}

but that didnt work either.

I then attempted with

$size = Get-ChildItem $name + "\folderName\" | Measure-Object -Property Length -sum

which was getting closer but i think my syntax is wrong with $name + "\folderName\" as im getting a series of errors. I say this is close as it has the path to the directory but it doesnt exist. The directory would exist if i can add the foldername to the $name variable?

Where am i going wrong? Or how could i retrieve the parent of each website folder size?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,612 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2021-11-06T13:13:50.3+00:00
    MotoX80 23,471 Reputation points

    This adds the count, and size in bytes to the output.

     $filepath = "C:\sites.csv"
     $sites = get-website
    
     foreach($site in $sites) {
         $name = $site.name
         $bindings = $site.bindings.collection.bindinginformation.split(":")
         $ip = $bindings[0]
         $port = $bindings[1]
         $hostHeader = $bindings[2]
         $size = Get-ChildItem ([system.Environment]::ExpandEnvironmentVariables($site.physicalPath)) -Recurse | Measure-Object -Property Length -sum
         "$name,$hostHeader,$ip,$port,$($size.count),$($size.sum)" | out-host
         "$name,$hostHeader,$ip,$port,$($size.count),$($size.sum)" | out-file $filePath -Append
     }
    
    No comments