SharePoint Online: Check if a Site Collection or Subsite Exists using PowerShell

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

How to check if a site exists in SharePoint Online?

Have you ever needed to check if a site exists in SharePoint Online? Maybe you’ve just been given the URL for a potential new site, and you want to make sure it’s already been created before starting to create it, or you’re writing a PowerShell script and need to verify that the site exists before continuing with your code. In either case, there are a few ways to check if a site exists. Let’s take a look at them!

To confirm if a site collection exists in SharePoint Online, You can quickly head on to:

  1. SharePoint admin center at https://<tenant>-admin.sharepoint.com >> Expand Sites >> Active Sites.
  2. Enter the site name or URL in the “Search sites” to check whether the site collection exists or not.sharepoint online powershell check if site exists

Alright, now I’ll show you how to use PowerShell to check if a SharePoint Online site exists.

SharePoint Online PowerShell CSOM to check if a site exists in the given URL:

How can you tell if a site already exists without having to search through all of your sites from the Admin center? PowerShell to the rescue! This PowerShell script checks if site collection exists in the given URL:

#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/suppliers"
 
#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
}
Else
{
    Write-Host "Site URL '$SiteURL' is not available!" -f Yellow
}

This is a convenient script when you are trying to automate your process and need to ensure the site exists before taking further action. How about checking whether a site or site collection exists through CSOM?

#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
    $Ctx.Load($web)
    
    Try {
            $Ctx.ExecuteQuery()
            Return $True
        }
    Catch [Exception] {
      Write-host $_.Exception.Message -f Red
      Return $False
     }        
}

#Variable for Site collection URL
$URL= "https://crescent.sharepoint.com/sites/sales/"

#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
 }
 else
 {
    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 the site exists in SharePoint Online

PowerShell is a powerful scripting language that you can use to automate tasks in Microsoft SharePoint Online. Let me show you how to use PnP PowerShell to check if a site exists in SharePoint Online:

#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/procurement"

Try
{
    #Connect to Tenant Admin
    Connect-PnPOnline -URL $AdminCenterURL -Interactive
     
    #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
    }
    Else
    {
        Write-host "Site $($SiteURL) exists already!" -foregroundcolor Green
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell to Check if a Subsite Exists in a Site Collection

#Parameters
$SiteCollURL = "https://crescent.sharepoint.com/sites/Retail"
$SubSiteURL = "/sites/Retail/archived/2010" #ServerRelativeUrl

Try
{
    #Connect to Tenant Admin
    Connect-PnPOnline -URL $SiteCollURL -Interactive
     
    #Get All Webs from the site
    $Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb

    #Check if site exists
    $Subsite = ($Webs | Select -ExpandProperty ServerRelativeUrl) -contains $SubSiteURL
    If ($Subsite -eq $false)
    {
        Write-host "SubSite $($SubSiteURL) doesn't exists!" -foregroundcolor Yellow
    }
    Else
    {
        Write-host "Site $($SubSiteURL) exists!" -foregroundcolor Green
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This can be useful for automation purposes or verifying whether a site is live (or has been deleted!). My other server-side script to Check if site collection or subsite exists in SharePoint using PowerShell in SharePoint on-premises.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

4 thoughts on “SharePoint Online: Check if a Site Collection or Subsite Exists using PowerShell

  • I would replace $Site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}
    $Site = Get-PnPTenantSite -Identity $SiteURL
    Because the first one will collect ALL sites in the tenant.

    Reply
  • 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… 🙂

    Reply
    • Agreed! The site could be inaccessible for a 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”.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *