When working with IIS administration through command line, AppCmd.exe list site
provides comprehensive site information, but often we need just the site name for scripting purposes. The output format follows this pattern:
SITE "SiteName" (id:123456,bindings:protocol/IP:port:,state:State)
Instead of complex string parsing, we can leverage PowerShell's text manipulation capabilities. Here's a robust solution:
$sites = & "$env:windir\system32\inetsrv\AppCmd.exe" list site
$siteNames = $sites | ForEach-Object {
if ($_ -match 'SITE "([^"]+)"') {
$matches[1]
}
}
$siteNames
The PowerShell script works through these steps:
- Captures raw output from AppCmd.exe
- Uses regex pattern matching to extract content between quotes
- Outputs clean site names only
For more complex environments, you might need these variations:
# Get sites in stopped state only
$sites | Where-Object { $_ -match 'state:Stopped' } | ForEach-Object {
if ($_ -match 'SITE "([^"]+)"') { $matches[1] }
}
# Export to CSV for documentation
$siteNames | Export-Csv -Path "sites.csv" -NoTypeInformation
Here's another method using PowerShell's Select-String cmdlet:
& "$env:windir\system32\inetsrv\AppCmd.exe" list site |
Select-String -Pattern '(?<=SITE ")[^"]+' -AllMatches |
ForEach-Object { $_.Matches.Value }
Always include basic error handling in production scripts:
try {
$sites = & "$env:windir\system32\inetsrv\AppCmd.exe" list site -ErrorAction Stop
$siteNames = $sites | ForEach-Object {
if ($_ -match 'SITE "([^"]+)"') {
$matches[1]
}
}
}
catch {
Write-Error "Failed to retrieve site list: $_"
}
When working with Windows Azure instances and IIS management through AppCmd.exe, developers often need to programmatically extract just the site name from the command output. The default output format contains more information than typically needed for automation scripts.
The standard AppCmd.exe list site
output follows this pattern:
SITE "SiteName" (id:123456,bindings:protocol/IP:port:,state:State)
For our specific case, we get:
SITE "MyCompany.MyProject.WebRole_IN_0_Web" (id:1273337555,bindings:https/555.555.555.555:443:,state:Started)
Here are several reliable methods to extract just the site name:
Method 1: Using PowerShell
$siteInfo = & "$env:windir\system32\inetsrv\AppCmd.exe" list site
$siteName = ($siteInfo -split '"')[1]
Write-Output $siteName
Method 2: Command Line with FINDSTR
for /f "tokens=2 delims=^"" %i in ('%windir%\system32\inetsrv\AppCmd.exe list site ^| findstr /i "SITE"') do @echo %i
Method 3: Advanced PowerShell (Multiple Sites)
Get-ChildItem IIS:\Sites | Select-Object -ExpandProperty Name
For production scripts, consider these additional scenarios:
- Sites containing quotation marks in their names
- Multiple sites in the output
- Different language versions of Windows
The PowerShell methods generally perform better for complex parsing scenarios, while the command-line version works well for simple single-site extraction.
$sites = Get-WmiObject -Namespace "root\WebAdministration" -Class Site
$sites | ForEach-Object { $_.Name }