SharePoint Online: Check if Site Collection, Subsite Exists using PowerShell CSOM

Requirement: Check if site or site collection exists in SharePoint Online using PowerShell CSOM.

SharePoint Online PowerShell CSOM to check if site or site collection exists in given URL:

This PowerShell script checks if site collection exists in the given URL.

#Set Parameters
$SiteURL = ""
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Get all site collections
$Sites = Get-SPOSite -Limit All | Select -ExpandProperty URL

#Check if any site with the URL exists already
If($Sites -notcontains $SiteURL)
    Write-Host "Site Collection URL '$SiteURL' is available!" -f Green
    Write-Host "Site URL '$SiteURL' is not available!" -f Yellow

How about checking if a site or site collection exists or not?

#Load SharePoint online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($SiteURL, $Credentials)
    #Setup context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.Web
    Try {
            Return $True
    Catch [Exception] {
      Write-host $_.Exception.Message -f Red
      Return $False

#Variable for Site collection URL
$URL= ""

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
$SiteExists = Check-SiteExists -SiteURL $URL -Credentials $Cred

#Call the function to check site exists
if($SiteExists -eq $True)
    write-host "Given Site Collection Exists!" -f Green
    #Proceed with your script
    write-host "Site Collection doesn't Exists on given URL!" -f Red

Please note, the function Check-SiteExists may return false in case of any network related issue or authentication failures.

PnP PowerShell to Check if site exists in SharePoint Online

$AdminCenterURL = ""
$SiteURL = ""
#Get Credentials to connect
$Cred = Get-Credential

    #Connect to Tenant Admin
    Connect-PnPOnline -URL $AdminCenterURL -Credential $Cred
    #Check if site exists
    $Site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}
    If ($Site -eq $null)
        Write-host "Site Collection $($SiteURL) doesn't exists!" -foregroundcolor Yellow
        Write-host "Site $($SiteURL) exists already!" -foregroundcolor Green
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

My another server side script to Check if site collection or subsite exists in SharePoint using PowerShell in SharePoint on-premises.

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “SharePoint Online: Check if Site Collection, Subsite Exists using PowerShell CSOM

  • August 27, 2018 at 8:39 AM

    Catching a generic exception of type Exception is very wrong, because if for example machine running this code lacks internet connection you will get an exception there saying that your network layer is not working yet your code will assume the SP web does not exist… I am pretty surprised that web is full of this kind of terrible examples… 🙂

    • August 27, 2018 at 9:20 AM

      Agreed! Site could be inaccessible for number of reasons such as network connectivity, credentials mismatch, etc. But the purpose of this function is to avoid proceeding with the rest of the script if it returns “Site doesn’t exist”.


Leave a Reply