21

I have a script that exports a site and restores it in another web application. I want to check if the target site already exists. Is there an easy way to tell if www.example.org/sites/asite/ is free, without throwing an error in Powershell?

Dan
  • 1,190
  • 3
  • 14
  • 25

2 Answers2

30

Using Powershell's ErrorAction you can suppress the error if a web does not exist.

$exists = (Get-SPWeb $url -ErrorAction SilentlyContinue) -ne $null
Steve P
  • 2,358
  • 17
  • 21
8

SharePoint 2010:

Get-SPWeb -Identity $url | Select-Object -Property Exists -ErrorAction SilentlyContinue

SharePoint 2007:

[void][reflection.assembly]::loadwithpartialname("Microsoft.SharePoint")
$siteCollection = New-Object Microsoft.SharePoint.SPSite($url)
$site = $siteCollection.OpenWeb()
Write-Host $site.Exists
...
$site.Dispose()
$siteCollection.Dispose()
Alexey Krasheninnikov
  • 2,750
  • 2
  • 24
  • 42