In some environments adding new virtual directories is a daily, if not hourly task. Automating this type of work is what we all prefer. Here’s some Powershell I’ve used recently to script IIS Virtual Directory setup. One thing about this script I like is it takes very little input, just the website name. The script pulls from existing settings to create the new virtual directory. If you had a single website, you wouldn’t even need this input.
This relies heavily on the WebAdministration Module. Most of the cmdlets are well documented on IIS.net. For more IIS Powershell examples, see my post on IIS Configuration Tips.
Script Highlights
- Check if Virtual Directory already exists, if present do nothing, if not present create.
- Find website’s physical path, use it for new virtual directory.
- Check if physical path exists, create if missing
- Find website’s application pool, use it for new virtual directory.
- Add a virtual directory under new application.
Script IIS Virtual Directory
Script IIS Virtual Directory