Saturday, October 11, 2014

Check if SharePoint Site Collection, Site, List, Document, Column Exists in PowerShell

Here is my nifty PowerShell scripts to check if the given Site collection, Site, List, Document (File) and column objects exists 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.

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= "http://demo.crescent.com/sites/operations" 

#Call the function to check site exists
if(Check-SiteExists($SiteURL))
 {
    write-host "Yes, Given Site Collection do Exists!" 
    #Proceed with your script
 }
Alternatively,
$SiteURL = "http://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 site exists in given URL:
#Custom Function to Check Site(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= "http://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
Lets check if 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= "http://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, lets check file exists in 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= "http://intranet.crescent.com"
$varFileURL = "http://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 do Exists!" 
    #Proceed with your script
 } 



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...