Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
in SharePoint Server by 7 8 11

I am using SharePoint 2016, and I have a small PowerShell Script that requires me to provide the SharePoint Site URL manually, so I need to check if the provided SharePoint Site URL already exists or not using PowerShell without raising errors.

I tried the below cmdlet to check if the SharePoint site exists

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

but I got this error if the provided SharePoint URL is not valid or the SharePoint site is not exists

Get-SPWeb : Cannot find an SPSite object that contains the following Id or Url: http://test.
At line:1 char:1
+ Get-SPWeb -Identity $url | Select-Object -Property Exists
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPip
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb

check if SharePoint site exists using PowerShell

How can I ensure that the provided SharePoint Site URL is a valid URL and the SharePoint site exists using PowerShell without showing this error if the SharePoint URL was not found or incorrect?

1 Answer

1 like 0 dislike
by 54 119 241
selected by
Best answer

Check if site exists in SharePoint using PowerShell Script

To check if a SharePoint Site exists using PowerShell, you have to use Get-SPWeb with -ErrorAction SilentlyContinue as the following:

$siteURL = "http://SiteURL"
$spWeb  = Get-SPWeb -site $siteURL -ErrorAction SilentlyContinue
if($spWeb -ne $null)
  write-host "$siteURL is found"
  write-host "$siteURL is not found"


how to Check if site exist in SharePoint using PowerShell Script

See Also

If you don’t ask, the answer is always NO!