How to Check if a SharePoint Site Exists using PnP PowerShell?

Recently, I was working on an automation script to create a SharePoint site. There, I wanted to check if a SharePoint site already exists. In this tutorial, I will show you how to check if a SharePoint site exists using PnP PowerShell.

Check if a SharePoint Site Exists using PnP PowerShell using Get-PnPTenantSite

I will show how to to check if a site exists in SharePoint using PnP PowerShell using Get-PnPTenantSite PnP PowerShell cmdlet.

In PnP PowerShell, you can use the Get-PnPTenantSite cmdlet to check if a site exists in SharePoint Online. The Get-PnPTenantSite cmdlet allows you to retrieve site collections from the tenant. You can use this cmdlet to check if a specific site exists.

Here is an example.

Connect-PnPOnline -Url "https://szg52.sharepoint.com" -Interactive

$siteUrl = "https://szg52.sharepoint.com/sites/accounting"
$site = Get-PnPTenantSite -Url $siteUrl -ErrorAction SilentlyContinue

if ($site) {
    Write-Output "The site 'accounting' exists."
} else {
    Write-Output "The site 'accounting' does not exist."
}

Since, in my tenant, there no site available as “accounting”, it shows output like “The site ‘accounting’ does not exist.”. You can check in the screenshot below.

Check if a SharePoint Site Exists using PnP PowerShell

You can also use the Where with the Get-PnPTenantSite cmdlet like below:

Connect-PnPOnline -Url "https://szg52.sharepoint.com" -Interactive

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
$site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}

if ($site) {
    Write-Output "The site 'PowerShellFAQs' exists."
} else {
    Write-Output "The site 'PowerShellFAQs' does not exist."
}

This is how to check if a SharePoint Online site exists or not using PnP PowerShell.

I hope this helps.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.