Check if SharePoint Site Collection, Site, List, Document, Column Exists in PowerShell
Here are my nifty PowerShell scripts to check if a given URL of the Site Collection, Site, List, Document (File), and column objects exist in SharePoint. You can utilize these helper functions wherever required.
PowerShell to check if Site Collection Exists:
Here is my custom function and function usage with PowerShell to check if site collection exists. SharePoint PowerShell to check if site collection exists:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($SiteURL)
{
return (Get-SPSite $SiteURL -ErrorAction SilentlyContinue) -ne $null
}
#Variable for Site collection
$SiteURL= "https://demo.crescent.com/sites/operations"
#Call the function to check site exists
if(Check-SiteExists($SiteURL))
{
write-host "Yes, Given Site Collection Exists!" -f Green
#Proceed with your script
}
else
{
write-host "No, Site Collection doesn't Exists on given URL!" -f Red
}
Alternatively, you can try:
$SiteURL = "https://portal.crescent.com/"
Try{
$Site=Get-SPSite $SiteURL -ErrorAction SilentlyContinue
}
catch{
write-host Site Collection with URL:$SiteURL Does not Exists!
return
}
PowerShell Script in to check if SharePoint Subsite exists in given URL:
#Custom Function to Check subsite(Web) Exists or Not
Function Check-WebExists($WebURL)
{
return (Get-SPWeb $WebURL -ErrorAction SilentlyContinue) -ne $null
}
SharePoint PowerShell to Check If List Exists:
#Function to Check if List Exists
Function Check-ListExists()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName )
Return (Get-SPWeb $SiteURL).lists.TryGetList($ListName) -ne $null
}
#Variable for Site collection
$varSiteURL= "https://intranet.crescent.com/sites/Sales"
$varListName = "Invoice"
#Call the function to check list exists
if(Check-ListExists -SiteURL $varSiteURL -ListName $varListName)
{
write-host "Yes, Given List do Exists!"
#Proceed with your script
}
Check if Site Column already exists using PowerShell
Let’s check if the site column exists using PowerShell:
#Function to Check if File Exists
Function Check-SiteColumnExists()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ColumnName )
Return (Get-SPWeb $SiteURL).Fields.ContainsField($ColumnName)
}
#Variable for Site collection
$varSiteURL= "https://intranet.crescent.com"
$varColumnName = "Department"
#Call the function to check list exists
if(Check-SiteColumnExists -SiteURL $varSiteURL -ColumnName $varColumnName)
{
write-host "Yes, Given Site Column do Exists!"
#Proceed with your script
}
SharePoint PowerShell check document exists
And finally, let’s check file exists in a given location:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Check if File Exists
Function Check-FileExists()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$FileURL )
Return (Get-SPWeb $SiteURL).GetFile($FileURL).Exists
}
#Variable for Site collection
$varSiteURL= "https://intranet.crescent.com"
$varFileURL = "https://intranet.crescent.com/Sales/Invoices/Inv-5060203.docx"
#Call the function to check list exists
if(Check-FileExists -SiteURL $varSiteURL -FileURL $varFileURL)
{
write-host "Yes, Given File does Exist!"
#Proceed with your script
}
Would you know how to check for the existence of a Document Set? Not been successful yet.