Share via


Check to see if a managed path exists using PowerShell

Here is a handy PowerShell function that will check to see if a managed path for a web application exists.  If it doesn't, it adds it.

 function AddManagedPath
{
      param(
      [String]$WebApplication, 
      [String]$ManagedPath,
      [Boolean]$Explicit
      )

      $WebApp = Get-SPWebApplication  -Identity $WebApplication

      Write-Host "Check to see if the managed path exists"

      $boolManagedPathExists = Get-SPManagedPath  -WebApplication $WebApp  -Identity $ManagedPath  -ErrorAction SilentlyContinue
      if($boolManagedPathExists -eq $null)
      {
            Write-Host "The managed path doesn't exist so it will be created"

            if($Explicit)
            {
                  New-SPManagedPath  -RelativeURL $ManagedPath  -WebApplication $WebApp  -ErrorAction SilentlyContinue
            }
            else
            {
                  New-SPManagedPath  -RelativeURL $ManagedPath  -WebApplication $WebApp  -ErrorAction SilentlyContinue  -Explicit
            }                 
      }
      else
      {
            Write-Host "Managed Path $ManagedPath already exists."
      }
      return
}
#Call the function which will check to see if the path exists.  If it doesn't it will add it.
AddManagedPath -WebApplication "ContosoWebApp" -ManagedPath "NewPath" -Explicit $false