Bulk Check if sites exist

Use PnP PowerShell to check if sites exist in bulk using a CSV file.

# Path to the input CSV file containing the list of site URLs
$inputCsvPath = "NotFound.csv"
# Path to the output CSV file where results will be written
$outputCsvPath = "NotFound-results.csv"

# Import the CSV file and store it as an array of site URLs
$sites = Import-Csv -Path $inputCsvPath

# Create an array to store results
$results = @()

# Loop through each site URL in the CSV file and check if it exists
foreach ($site in $sites) {
    $siteUrl = $site.URL
    $result = [PSCustomObject]@{
        SiteUrl   = $siteUrl
        ExistsInTenant = $false
    }

    try {
        # Check if the site exists by using Get-SPOSite
        $siteCheck = Get-SPOSite -Identity $siteUrl -ErrorAction Stop

        # If the siteCheck is successful, the site exists
        $result.ExistsInTenant = $true
        Write-Host "Site exists: $siteUrl"
    }
    catch {
        # If an error occurs, assume the site does not exist
        Write-Warning "Site does not exist: $siteUrl"
    }

    # Add the result to the results array
    $results += $result
}

# Export the results to a CSV file
$results | Export-Csv -Path $outputCsvPath -NoTypeInformation

Write-Host "Completed checking sites. Results written to $outputCsvPath"