0
# CREATE COMMUNICATION SITE
New-PnPSite `
  -Type CommunicationSite `
  -Title $team `
  -Url ("https://hiddenforsecurity.sharepoint.com/sites/" + $team) `
  -SiteDesign 'Blank' 

I need to wrap this in a if statement that will check if the site is already created, can anybody point me in the right direction?

TY :)

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
BennKingy
  • 436
  • 1
  • 6
  • 28

2 Answers2

4

You can use the Get-PnPTenantSite command to check if a site collection exists or not as below:

$site = ""
Try
{
    Write-Host "Checking, if site already exists..."

    $site = Get-PnPTenantSite -Url "<your-site-url>" -ErrorAction SilentlyContinue
}
Catch
{    
}

if ($site -ne $null)
{
    Write-Host "Site already exists, use existing..."
}
else
{
    Write-Host "Site doesn't exist, creating new..."
}
Gautam Sheth
  • 30,881
  • 1
  • 35
  • 62
1

Use something like below:

$site = Get-SPOWeb -Identity "Site" -ErrorAction SilentlyContinue
if ($subsite -eq $null) {
  Write-Host "Site does not exist, create..." -ForegroundColor Yellow
}
else {
  Write-Host "Subsite already exist" -ForegroundColor Green
}

Reference:

SharePoint Online: Site Operation with PnP Powershell.

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61