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

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 
            
            }

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.


Thank you @Mathias R. Jessen , I forgot about this post and I should've came back because I figured most of it out. Your script works perfect and here's mine just for S&Gs, didn't realize all I had to do was call on the objects and properties.

$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] "::: is the Virtual Directory" -ForegroundColor Green
                                        Write-Host $webvdirectory.physicalPath   
                                        Write-Host $webvdirectory.userName                                         
                                        Write-Host $webvdirectory.password
                                        Write-Host $webvdirectory.logonMethod
                                    } 
                            #Write-Host $VDir 

            }

Also have another problem though and didn't know if you had the answer, how would I call on the Physical Path credentials property for web applications? I cant print out the username or password, but I can print the logonMethod which uses the same property? below is my code for web applications, it is very similar to my virtual directories code:

$Websites = Get-ChildItem IIS:\Sites 
    foreach($Site in $Websites)
            { 
             $webapps = Get-WebApplication -Site $Site.name 
                    foreach($webapp in $webAPPS)
                        {
                        Write-Host $webapp.applicationPool
                        Write-Host $WebApp.virtualDirectoryDefaults.userName
                        Write-Host $webapp.virtualDirectoryDefaults.password
                        Write-Host $webapp.virtualDirectoryDefaults.logonMethod
                        $webapp2 = $webapp.path
                        Write-Host $WebApp2.split("/")[-1] "::: is the Web Application" -ForegroundColor Green

                        } 
            }