Powershell – How to list out Virtual Directories in IIS from the all the websites in PowerShell

iis-8powershellvirtual-directorywindows-server-2012

I have hit a complete roadblock and cannot figure out how to print out the virtual directories for each website in IIS 8. I keep retrieving Applications and empty null argument errors and just can't find a way around to get the virtual directories to list out.

I need the following data to list out about the Virtual Directories:

  • Physical Path
  • App Pool Association
  • Site Name
  • Credentials

Below is the code I have been using and messing around with:

    *See revised code below*
    

I have also used the Appcmd.exe command to try and to list out the Virtual Directories:

C:\Windows\System32\inetsrv\appcmd.exe LIST VDIRS

But I am still not getting the output I need. Anything or any help would be highly appreciated! Thanks!

EDIT#———————————————————————-

Got my virtual directories to list out by commenting out a couple lines but it only prints out the name and the:

  • Physical Path
  • App Pool Association
  • Site Name
  • Credentials

are still unknown to me, below is the revised code:

  Import-Module WebAdministration
    $Websites = Get-ChildItem IIS:\Sites 
    foreach($Site in $Websites)
            {
                $webapps = Get-WebApplication -Site $Site.name 
                $VDir = Get-WebVirtualDirectory -Site $Site.name #-Application $webapps.name
                                foreach($webvdirectory in $VDir)
                                    {
                                        $webvdirectory2 = $webvdirectory.path
                                        Write-Host $webvdirectory2.split("/")[-1]
                                        #Write-Host $webvdirectory
                                        #Write-Host $webvdirectory.name
                           
                                    } 
                            #Write-Host $VDir 
            
            }

Best Answer

You write that you can't figure out how to retrieve:

  • Physical Path
    • is directly accessible through the physicalPath noteproperty
  • App Pool Association
    • doesn't make sense, Applications are assigned to app pools, directories themselves are not
  • Site Name
    • You already know this ($Site.name)
  • Credentials
    • I assume you just want the username if present

These could all be retrieved with some slight alterations to your existing script:

Import-Module WebAdministration

$Websites = Get-ChildItem IIS:\Sites 

$AllVDirs = @()

foreach($Site in $Websites)
{
    $VDirs = Get-WebVirtualDirectory -Site $Site.name
    foreach($webvdirectory in $VDirs)
    {
            $vdir = New-Object psobject -Property @{
                "Name" = ($webvdirectory.path -split "/")[-1]
                "Site" = $Site.name
                "Path" = $webvdirectory.path
                "PhysicalPath" = $webvdirectory.physicalPath
                "PhysicalPathCredentials" = $webvdirectory.userName
            }

            $AllVDirs += $vdir
    } 
}

$AllVDirs

Now you can export $AllVDirs to Xml, Csv or simply print it to the PowerShell host.