Wednesday, June 22, 2016

Powershell to Export data of Site Collection or Sub Site in a one Go from SharePoint Onprem.

Powershell to export all data of onprem sharePoint (SP2010/2013) site or site collection in one go.


Run that below Powershell in SharePoint Management studio on SP Onprem server(SP 2010/2013)

Note -
Make sure having sufficient space in C drive,exported data resides  in C:\temp
If not having sufficient space in C drive,You can change the location for exported data.

-------------------------------------------------------------------------------------------------------------------

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"


$OutputFileLocation = "C:\temp\DocLib.csv"
Start-Transcript -path $OutputFileLocation -append
 $LogFilePath = "C:\temp\ErrorLog.log"
 $DATE = get-date


 Function exportDocLib($siteurl){
#Get site collection details
#$s = Get-SPSite http://stockxchange.stockland.com.au
#Get all web sites in the site collections
#$wc = $siteurl.AllWebs | ?{$_.url -like "*" + $subsiteName + "*"}
$wc = $siteurl.AllWebs
try{
foreach($web in $wc)
{
#loopthrouh the lists and libraries in the site
#Write-Host "Iterating Site " $web.Url
foreach($list in $web.Lists)
  {
        #Check for only document libraries
        #if($list.BaseTemplate -eq "DocumentLibrary")
        #{      
            Write-Host $list.Title"(Web: "$web.Title")--(webname:"$web.Name")"           
            $srcpath=$web.Url+"/"
            Write-Host "source path is :" $srcpath
            Write-Host 'Checking InternalName is:'     $list.Url
            $path = $list.Url.Substring(36)       
            Write-Host "library path is: " $path
            $itempath=$path+"/"
            Write-Host "New itempath is ::::::"$itempath          
            $folderpath=$path.replace('/','\')
            $filepath="C:\temp"+$folderpath
            Write-Host "Export Location filepath is:"  $filepath
          
            Export-SPWeb -Identity $srcpath -ItemUrl $itempath -Path $filepath -NoFileCompression -IncludeVersions "All"      
      
        #}  
    }
}
}
catch
    {
            Write-Host "Error occured while Exporting List and Libraries. See log file for more details."
            $format= "==================================================================================================="  
            $format | out-file $LogFilePath -append
            $Date | out-file $LogFilePath -append
            "Error occured while Exporting List and Libraries. See log file for more details." | out-file $LogFilePath -append
            "Site Collection URL: "+$siteurl |out-file $LogFilePath -append    
            "List Title: "+$list.Title | out-file $LogFilePath -append
            $("Error caught: " + $_.Exception.GetType().FullName) | out-file $LogFilePath -append
            $("Error caught: " + $_.Exception.Message) | out-file $LogFilePath -append
            $format | out-file $LogFilePath -append
    }

 }




 Function export($web){
try{
    foreach($list in $web.Lists){
        $format= "==================================================================================================="  
        $format | out-file $LogFilePath -append
        $Date | out-file $LogFilePath -append

        Write-Host "ListName:::" $list.Title
        $srcpath=$web.Url+"/"
        Write-Host "sourcepath is ::::::" $srcpath
        Write-Host 'Checking InternalName is: '     $list.Url
        $path = $list.Url.Substring(36)       
        Write-Host "library path is:" $path
        $itempath=$path+"/"
        Write-Host "New itempath is ::::::"$itempath
      
        $folderpath=$path.replace('/','\')
        $filepath="C:\temp"+$folderpath
        Write-Host "Export Location filepath is:"  $filepath      
        Export-SPWeb -Identity $srcpath -ItemUrl $itempath -Path $filepath -NoFileCompression -IncludeVersions "All"
  
    }
}catch{
        Write-Host "Error occured while Exporting List and Libraries. See log file for more details."
        $format= "==================================================================================================="  
        $format | out-file $LogFilePath -append
        $Date | out-file $LogFilePath -append
        "Error occured while Exporting List and Libraries. See log file for more details." | out-file $LogFilePath -append
        "Site URL: "+$web |out-file $LogFilePath -append    
        "List Title: "+$list.Title | out-file $LogFilePath -append
        $("Error caught: " + $_.Exception.GetType().FullName) | out-file $LogFilePath -append
        $("Error caught: " + $_.Exception.Message) | out-file $LogFilePath -append
        $format | out-file $LogFilePath -append
    }
 }



 $inputsiteurl=  Read-Host -Prompt 'Input your sitecollection or site url here'
 $siteurl=Get-SPSite  $inputsiteurl  -ErrorAction SilentlyContinue -ErrorVariable err

 if($siteurl -eq $Null)
{  
    $weburl=Get-SPWeb  $inputsiteurl
    Write-Host "spweb is" $weburl
    export $weburl
}
else
{
    Write-Host "spsite is" $siteurl
    exportDocLib $siteurl
}

 #$subsiteName = Read-Host - Prompt 'Input Sub Site URL'

Stop-Transcript

3 comments:

  1. Very helpful script. Thanks for that. I just had to change $list.Url to $list.Rootfolder.Url for a SharePoint 2013 Export. After that change it worked perfect!

    Best Regards
    Christof

    ReplyDelete