Q:

Powershell script not creating site folder

This is powershell script to create IIS site and application pool. It creates site and application pool successfully but i dont see sitefolder been created. why?
$iisAppPoolName = "webtest.abc.com"
$iisAppPoolDotNetVersion = "v4.0"
$iisAppPoolManagedPipeLineMode = "0"
$iisAppName = "webtest.abc.com"
$directoryPath = "C:\inetpub\wwwroot\" + $iisAppPoolName
$SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath  $iisAppName

#navigate to the app pools root
cd IIS:\AppPools\

#check if the app pool exists
if (!(Test-Path $iisAppPoolName -pathType container))
{
    #create the app pool
    $appPool = New-Item $iisAppPoolName
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
    $appPool | Set-ItemProperty -Name "managedPipelineMode" -Value $iisAppPoolManagedPipeLineMode

}

#navigate to the sites root
cd IIS:\Sites\

 #check if the site exists
 if (Test-Path $iisAppName -pathType container)
 {
   return
 }

 #create the site
 $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" + $iisAppName} -physicalPath  $SiteFolder
 $iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

A:

Seems like when you define site properties this way there is no check for directory existance. When you do the same from GUI you would get error saying The specified directory does not exist on the server.
You could create folder just before you create the site. Like so:
#create the site
New-Item -Path $SiteFolder -ItemType Directory | Out-Null
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" + $iisAppName} -physicalPath  $SiteFolder
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

