Update IIS virtual directory physical paths

Powershell

This is for cases where we have an IIS webdav server that has virtual directories corresponding to each user. If a particular site in the organization changes the file server where its user home folders reside, it becomes necessary to mass update any physical paths that refer to the old server.

Import-Module WebAdministration
$collectCampus = get-webvirtualdirectory | where {$_.Physicalpath -like "\\foo\users\*"}
foreach ($i in $collectCampus){
  $strPhysicalPath = [string] $i.Physicalpath
  $strPhysicalPath = $strPhysicalPath -replace "foo", "bar" #server name swap
  $strPhysicalPath = $strPhysicalPath -replace "users", "userdata" #share name swap (working around backslash)
  $strName = [string] $i.Path
  $strName = $strName -replace "/", "" #removing leading / from Path value
  $strIISPath = "IIS:\Sites\Default Web Site\" + $strName
  #SAMPLE: Set-ItemProperty 'IIS:\Sites\Default Web Site\DemoVirtualDirectory' -Name physicalPath -Value C:\inetpub\newDemoVirtualDirectory
  Set-ItemProperty $strIISPath -Name physicalPath -Value $strPhysicalPath
}

Leave a comment