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?
Asked
Active
Viewed 3.3k times
2 Answers
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
Get-SPWeb -Identity 'http://example.org' | Select-Object -Property Exists
The 2007 version doesn't throw errors but returns true for sites that don't exist.
– Dan May 16 '11 at 06:28