IIS Server Details Export to CSV Using PowerShell
IIS Server Details Export to CSV Using PowerShell
This PowerShell script collects IIS website information and exports it to a CSV file. It is useful when you need a quick inventory of websites hosted on a Windows IIS server.
Requirement: Run PowerShell as Administrator and make sure the IIS WebAdministration module is available.
What This Script Collects
- Server name
- IIS website name and ID
- Website status
- URL and binding information
- IP address, port, and host header
- Website physical path
- Whether the folder exists
- Folder size in GB
- Last modified time
- Application Pool
1. Complete PowerShell Script
Save the script as Export-IISInventory.ps1:
# IIS Website Inventory Export
Import-Module WebAdministration
# Output folder
$OutputFolder = "C:\Temp"
if (!(Test-Path $OutputFolder)) {
New-Item -ItemType Directory -Path $OutputFolder | Out-Null
}
$OutputFile = Join-Path $OutputFolder "IIS_Website_Inventory.csv"
$Results = @()
foreach ($Site in Get-Website)
{
try
{
$SiteItem = Get-Item ("IIS:\Sites\" + $Site.Name)
$PhysicalPath = $SiteItem.physicalPath
}
catch
{
$PhysicalPath = ""
}
try
{
$AppPool = $Site.applicationPool
}
catch
{
$AppPool = ""
}
foreach ($Binding in $Site.Bindings.Collection)
{
$Protocol = $Binding.protocol
$BindingInfo = $Binding.bindingInformation.Split(':')
$IPAddress = $BindingInfo[0]
$Port = $BindingInfo[1]
$HostHeader = ""
if ($BindingInfo.Count -ge 3)
{
$HostHeader = $BindingInfo[2]
}
# Build URL
if (![string]::IsNullOrWhiteSpace($HostHeader))
{
if (($Protocol -eq "http" -and $Port -eq "80") -or
($Protocol -eq "https" -and $Port -eq "443"))
{
$URL = $Protocol + "://" + $HostHeader
}
else
{
$URL = $Protocol + "://" + $HostHeader + ":" + $Port
}
}
else
{
$ServerName = $env:COMPUTERNAME
$URL = $Protocol + "://" + $ServerName + ":" + $Port
}
# Check folder
$FolderExists = Test-Path $PhysicalPath
# Calculate folder size
$FolderSizeGB = ""
if ($FolderExists)
{
try
{
$Size = (Get-ChildItem $PhysicalPath -Recurse -File -ErrorAction SilentlyContinue |
Measure-Object Length -Sum).Sum
if ($Size)
{
$FolderSizeGB = [Math]::Round($Size / 1GB, 2)
}
}
catch
{
$FolderSizeGB = ""
}
}
# Last modified
$LastModified = ""
if ($FolderExists)
{
try
{
$LastModified = (Get-Item $PhysicalPath).LastWriteTime
}
catch
{
}
}
$Results += [PSCustomObject]@{
ServerName = $env:COMPUTERNAME
SiteName = $Site.Name
SiteID = $Site.ID
Status = $Site.State
URL = $URL
Protocol = $Protocol
IPAddress = $IPAddress
Port = $Port
HostHeader = $HostHeader
PhysicalPath = $PhysicalPath
FolderExists = $FolderExists
FolderSizeGB = $FolderSizeGB
LastModified = $LastModified
ApplicationPool = $AppPool
}
}
}
$Results |
Sort-Object SiteName, Protocol |
Export-Csv $OutputFile -NoTypeInformation -Encoding UTF8
Write-Host ""
Write-Host "==============================================="
Write-Host "Export Completed Successfully"
Write-Host "==============================================="
Write-Host ""
Write-Host "CSV File:"
Write-Host $OutputFile
Write-Host ""
Write-Host "Total Websites :" ($Results | Select-Object SiteName -Unique).Count
Write-Host "Total Bindings :" $Results.Count
Write-Host ""2. How It Works
Load the IIS Module
Import-Module WebAdministration
This loads the PowerShell module used to manage and read IIS configuration.
Set the Output File
$OutputFolder = "C:\Temp" $OutputFile = Join-Path $OutputFolder "IIS_Website_Inventory.csv"
The CSV report is saved in C:\Temp.
Read IIS Websites
foreach ($Site in Get-Website)
The script loops through every IIS website configured on the server.
Read Bindings
foreach ($Binding in $Site.Bindings.Collection)
A website can have multiple HTTP or HTTPS bindings, so the script exports each binding separately.
Calculate Folder Size
Get-ChildItem $PhysicalPath -Recurse -File
The script calculates the size of files under the website's physical path.
Export to CSV
$Results | Sort-Object SiteName, Protocol | Export-Csv $OutputFile -NoTypeInformation -Encoding UTF8
All collected information is sorted and written to the CSV report.
3. Run the Script
Open PowerShell as Administrator and run:
Set-ExecutionPolicy -Scope Process Bypass .\Export-IISInventory.ps1
The report will be created as:
C:\Temp\IIS_Website_Inventory.csv
4. Example Output
The CSV contains fields such as:
ServerName SiteName SiteID Status URL Protocol IPAddress Port HostHeader PhysicalPath FolderExists FolderSizeGB LastModified ApplicationPool
Quick Reference
# Check IIS websites Get-Website # Check IIS PowerShell module Get-Module -ListAvailable WebAdministration # Run the inventory script .\Export-IISInventory.ps1
Important Note
If an IIS website has a very large number of files, calculating the complete folder size with a recursive file scan can take some time. The script also uses the physical folder path configured for each IIS website.
Conclusion
This script provides a simple IIS inventory report containing websites, bindings, application pools, physical paths, folder status, folder size, and other useful server details in a single CSV file.
Discussion (0)