How to Get the Physical Path of an IIS Website in Batch Script
When managing IIS servers, knowing the physical file system path of a website is essential for deployments, backups, troubleshooting, and configuration audits. While you can look this up in the IIS Manager GUI, scripting the retrieval allows you to integrate it into automated workflows that need to locate, copy, or modify website files programmatically.
In this guide, we will explore how to retrieve the physical path of IIS websites and virtual directories using the appcmd.exe command-line tool within a Batch Script.
Method 1: Getting the Physical Path of a Specific Website
The physical path is stored in the virtual directory (vdir) configuration of the site's root application.
@echo off
setlocal
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "site_name=CompanyWebsite"
:: Verify Admin
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)
echo Getting physical path for "%site_name%"...
set "site_path="
for /f "tokens=*" %%P in ('"%appcmd%" list vdir /app.name:"%site_name%/" /text:physicalPath 2^>nul') do set "site_path=%%P"
if defined site_path (
echo.
echo Site: %site_name%
echo Physical Path: %site_path%
) else (
echo [ERROR] Site "%site_name%" not found or has no root virtual directory.
)
pause
endlocal
Understanding the Query
list vdir: Lists virtual directories./app.name:"%site_name%/": Filters by the root application of the specified site (note the trailing/)./text:physicalPath: Returns only thephysicalPathproperty as plain text.
Method 2: Listing All Websites with Their Physical Paths
For a complete server inventory:
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo =============================================
echo IIS WEBSITES AND PHYSICAL PATHS
echo =============================================
echo.
for /f "tokens=*" %%N in ('"%appcmd%" list site /text:name') do (
set "name=%%N"
set "path="
for /f "tokens=*" %%P in ('"%appcmd%" list vdir /app.name:"!name!/" /text:physicalPath 2^>nul') do set "path=%%P"
echo !name!
if defined path (
echo Path: !path!
) else (
echo Path: (not configured^)
)
echo.
)
pause
endlocal
Sample Output
Default Web Site
Path: C:\inetpub\wwwroot
CompanyBlog
Path: D:\WebApps\Blog
StagingApi
Path: E:\Staging\API\wwwroot
Method 3: Storing the Path in a Variable for Use in Scripts
The most common use case is retrieving the path and then using it for operations like file copy, backup, or permission changes.
@echo off
setlocal
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "site_name=ProductionApp"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
:: Get the path into a variable
set "web_root="
for /f "tokens=*" %%P in ('"%appcmd%" list vdir /app.name:"%site_name%/" /text:physicalPath 2^>nul') do set "web_root=%%P"
if not defined web_root (
echo [ERROR] Could not determine physical path.
pause
exit /b 1
)
echo Web root: %web_root%
:: Verify the path exists on disk
if not exist "%web_root%\" (
echo [ERROR] Physical path does not exist: %web_root%
pause
exit /b 1
)
:: Example: Create a backup of the site
set "backup_dir=D:\Backups\%site_name%_%date:~-4%%date:~4,2%%date:~7,2%"
echo Backing up to: %backup_dir%
xcopy "%web_root%\*" "%backup_dir%\" /s /e /y /q >nul
if %errorlevel% equ 0 (
echo [OK] Backup complete.
) else (
echo [ERROR] Backup failed.
)
pause
endlocal
Method 4: Getting Paths for All Virtual Directories
A website may have multiple applications, each with its own virtual directories and physical paths.
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "site_name=CompanyIntranet"
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Virtual directories for "%site_name%":
echo =======================================
echo.
set "count=0"
:: List all applications under this site, then their virtual directories
for /f "tokens=*" %%A in ('"%appcmd%" list app /site.name:"%site_name%" /text:app.name 2^>nul') do (
for /f "tokens=*" %%P in ('"%appcmd%" list vdir /app.name:"%%A" /text:physicalPath 2^>nul') do (
echo Application: %%A
echo Physical Path: %%P
echo.
set /a "count+=1"
)
)
if !count! equ 0 (
echo No virtual directories found for "%site_name%".
)
pause
endlocal
Method 5: CSV Export of All Sites and Paths
For inventory and documentation:
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "output=%~dp0site_paths_%COMPUTERNAME%.csv"
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Exporting site paths to CSV...
echo "SiteName","PhysicalPath","State","Bindings" > "%output%"
for /f "tokens=*" %%N in ('"%appcmd%" list site /text:name') do (
set "name=%%N"
set "path="
set "state="
set "bindings="
for /f "tokens=*" %%P in ('"%appcmd%" list vdir /app.name:"!name!/" /text:physicalPath 2^>nul') do set "path=%%P"
for /f "tokens=*" %%S in ('"%appcmd%" list site /name:"!name!" /text:state 2^>nul') do set "state=%%S"
for /f "tokens=*" %%B in ('"%appcmd%" list site /name:"!name!" /text:bindings 2^>nul') do set "bindings=%%B"
echo "!name!","!path!","!state!","!bindings!" >> "%output%"
)
echo [SUCCESS] Exported to: %output%
echo.
type "%output%"
echo.
pause
endlocal
Practical Use Case: Disk Usage Analysis
Knowing the physical path allows you to check how much disk space each website consumes:
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo =============================================
echo IIS WEBSITE DISK USAGE
echo =============================================
echo.
for /f "tokens=*" %%N in ('"%appcmd%" list site /text:name') do (
set "name=%%N"
set "path="
for /f "tokens=*" %%P in ('"%appcmd%" list vdir /app.name:"!name!/" /text:physicalPath 2^>nul') do set "path=%%P"
if defined path (
if exist "!path!\" (
echo !name!: !path!
:: Get folder size using PowerShell
for /f %%S in ('powershell -NoProfile -Command ^
"[math]::Round((Get-ChildItem '!path!' -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1MB, 2)" 2^>nul') do (
echo Size: %%S MB
)
) else (
echo !name!: !path! [PATH NOT FOUND]
)
) else (
echo !name!: (no physical path configured^)
)
echo.
)
pause
endlocal
Using PowerShell as an Alternative
For environments where the WebAdministration module is available:
@echo off
echo IIS Site Paths (via PowerShell^):
echo.
powershell -NoProfile -Command ^
"Import-Module WebAdministration -ErrorAction SilentlyContinue;" ^
"Get-Website | Select-Object Name, PhysicalPath, State | Format-Table -AutoSize"
pause
Common Mistakes
The Wrong Way: Parsing applicationHost.config with findstr
:: WRONG - XML parsing with findstr is unreliable
findstr "physicalPath" %SystemRoot%\System32\inetsrv\config\applicationHost.config
Output Concern:
findstr returns every line containing "physicalPath" from the raw XML, including virtual directories, applications, and default configurations mixed together. The output has no context about which site or application each path belongs to. Always use appcmd which returns structured, contextualized results.
The Wrong Way: Assuming All Sites Use C:\inetpub
:: WRONG - Hardcoding the default path
set "web_root=C:\inetpub\wwwroot\%site_name%"
Administrators regularly configure sites on different drives, network shares, or non-standard paths. Hardcoding C:\inetpub will cause scripts to operate on the wrong directory.
Best Practices
- Always query dynamically: Use
appcmd list vdirto get the actual configured path rather than hardcoding assumptions. - Include the trailing slash in app names: The root application is referenced as
SiteName/with a trailing slash. - Validate path existence: After retrieving the path, verify it exists with
if existbefore performing file operations. - Export for documentation: Regularly export site-to-path mappings to CSV for disaster recovery documentation and audit compliance.
Conclusion
Retrieving the physical path of an IIS website from a Batch Script is done by querying the virtual directory configuration with appcmd list vdir and extracting the physicalPath property. This seemingly simple query underpins critical automation tasks including site backups, deployment scripts, disk usage analysis, and server inventory reports. By dynamically querying the path instead of hardcoding it, scripts remain portable and resilient across different server configurations and organizational directory structures.