Home > SharePoint 2010, SharePoint Administration > Export All List/Document Libraries of a Specific Site Collection in SharePoint 2010 Using PowerShell

Export All List/Document Libraries of a Specific Site Collection in SharePoint 2010 Using PowerShell

I recently had to do an export of all Lists and Document libraries of  a specific Site Collection in my SharePoint 2010 Farm due to a corrupted Site Collection.  I needed to move all these list and documents into a new Clean Site Collection.  Since the Site Collection had quite a few Lists and Document Libraries I did not want to use the Export functionality within Central Administrations.  That would have taken forever.  So I turned to my beloved PowerShell  to help out.  I had to iterate through the entire lists and document libraries of the Site Collection, grab the relative URL and then export.  Below is the script I wrote to accomplish this.

—————————————————————————————————————————————————-

#This is the source web that is hosting the lists to export
$sourceURL = Read-Host “Enter in the Source Site that is hosting the lists and document libraries to export”

#Location to store the export file
$path = Read-Host “Location to store exported list”

#Select the Title and itemURL from Lists and Document Libraries and store in $list variable
$lists = @($(Get-SPWeb -Identity $sourceURL).lists | Select-Object Title, @{Name=”itemURL”;Expression = {$_.RootFolder}})

Write-Host “Exporting Lists & Document Libraries from ” $sourceURL -ForegroundColor Cyan

#Loop through each list and document library and Export
foreach($list in $lists)

{

Write-Host “Exporting ” $list.Title

Export-SPWeb -Identity $sourceURL -ItemURL $list.itemURL -path ($path + “\” + $list.Title + “.cmp”) -IncludeVersions All -IncludeUserSecurity

}

Write-Host ‘Export Complete” -ForegroundColor Cyan

$sourceWebURL.Dispose()


***Note***

For me I had to do some extra plumbing to some list and document library Titles before export.  Since the export was using the exact same Title as what the list and document library’s were named upon creation in SharePoint, some of my lists and document libraries had invalid characters which prevented me from exporting.

Since I knew what the invalid character was I just simply did a .Replace to replace the invalid character with a space, such as:

#Initialize new variable to hold the $list.Title
$TitleRename = $list.Title
#Perform a Replace to search for “:” in the string and replace with a space “”.
$TitleRename = $TitleRename.Replace(“:”,””)

So instead of using the Export command I used above I used:

Export-SPWeb -Identity $sourceURL -ItemURL $list.itemURL -path ($path + “\” + $TitleRename + “.cmp”) -IncludeVersions All -IncludeUserSecurity

Advertisements
  1. Beth Perkins
    June 25, 2012 at 9:33 pm

    Hey, thanks for the code – I’m trying to implement this to do an export, but the $list.itemURL is not being grabbed properly. I tried Write-Host $list.itemURL, and the list was blank. When I tried Write-Host $list.Title, I got the list of titles. At any rate, I can run this just fine, but I get a backup of the entire set of site lists every time, from what I can tell. Any ideas? Thanks!

    • June 26, 2012 at 9:04 am

      Hi Beth,

      Do you mind if I took a look at how you have your script written? Maybe I can understand better on what you might be missing.

      Thanks

      v/r
      JShidell

      • Beth Perkins
        June 26, 2012 at 1:19 pm

        Hi J,

        Here’s the code:

        Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue

        $today = (Get-Date -Format MM-dd-yyyy)
        $sourceURL = “http://nsvg002/”
        #[IO.Directory]::CreateDirectory(“\\nsvg001\lock\Internal Computer Systems\SharePoint Backups\NSVG002\Individual Site Library Backups\$today”)
        $path = “\\nsvg001\lock\Internal Computer Systems\SharePoint Backups\NSVG002\Individual Site Library Backups\$today”
        $lists = @($(Get-SPWeb -Identity $sourceURL).lists | Select-Object Title, @{Name=”itemURL”;Expression = {$_.RootRolder}})

        #Write-Host “Exporting Lists & Document Libraries from ” $sourceURL -ForegroundColor Cyan
        foreach($list in $lists)
        {
        Export-SPWeb -Identity http://nsvg002/ -ItemURL $librname -path ($path + “\” + $list.Title + “.cmp”) -IncludeUserSecurity
        }
        $sourceWebURL.Dispose();

  2. Beth Perkins
    June 27, 2012 at 7:51 pm

    I actually found a fix to this and it involved a couple of things:

    Here’s the code that worked for me:

    $web = Get-SPWeb “http://nsvg002/”
    $path = “\\nsvg001\lock\Internal Computer Systems\SharePoint Backups\NSVG002\Individual Site Library Backups\$today\”

    foreach($list in $web.lists)
    {
    “Exporting ” + $list.RootFolder.URL
    export-spweb $web.URL -ItemUrl (“/” + $list.RootFolder.URL) -IncludeUserSecurity -IncludeVersions All -path ($path + $list + “.cmp”) -nologfile
    }

  3. Pete
    October 26, 2012 at 12:03 pm

    Neither jshidell nor Beth’s code work for me and the code is quite different. I’ve been searching for a powershell script to export all document libraries in the site collection to csv but,in all cases, the poweshell code is very different. Isn’t there a standard way to do things in Powershell? – instead of having to try completely different code when attempting the same thing!

  4. October 26, 2012 at 2:22 pm

    Hi Pete,

    Question is there a reason why you would export your document libraries to csv? It should be exported to .cmp.

    This script exports the entire document library (content and permissions) to allow you to restore to a different site collection or location later.

    Exporting to a .csv will never be successful in restoring or importing and this script will not work for that.

    You can export to .csv if you want to capture the document library content. Is that what you are trying to accomplish?

    • Pete
      October 26, 2012 at 2:45 pm

      Yes, I just want to export the listitem details from all doc libraries in the site collection to a csv for import to SQL Server (for subsequent reporting with SSRS). I know next to nothing about PowerShell and am searching for scripts that I could maybe adapt for my use. But what look like usable solutions are usually very different from each other and it’s difficult to fathom the best way to do it

      • October 26, 2012 at 3:00 pm

        Hi Pete,

        So you would like to export the List Item details i.e. (Title, Description, etc) all the meta data files into excel?

        Give me a little while I should be able to come up with what you need.

        Thanks

        v/r
        JShidell

      • October 26, 2012 at 3:43 pm

        Also just curious, is there a reason why you don’t want to export your list via the export-to-excel functionality within SharePoint?

  5. Pete
    October 29, 2012 at 10:22 am

    I want to automate the extract to run out of hours, import to SQL Server and report with SSRS as a scheduled daily process (wthout manual intervention).

  6. Pete
    November 1, 2012 at 3:09 pm

    I’ve now managed to do this using a recursive function to loop all sub sites and export the required six taxonomy columns to csv along with the site name. Had some frustrations until I realised that the SharePoint display and internal names are often not the same! We can now run the export out of hours, import the csv to SQL Server and then run our SSRS reports. Not much processing overhead as the process is mainly external to SharePoint. All this because we had not originally set the taxonomy columns to ‘required’.

    • November 1, 2012 at 3:21 pm

      Pete – Glad you were able to figure this out. Yes you have to watch out for the display and internal names, I ran into that problem just recently when I was trying to loop through all my SharePoint List Views and comparing each list view column to specific meta data columns.

      v/r
      JShidell

  7. Marco
    March 11, 2013 at 7:23 pm

    In fact there were a couple of typos that looks like are in purpose.

    At the end it helped me as I had to research a bit and at the end I learned new things.

    Like…RootRolder instead of RootFolder…

    • March 12, 2013 at 1:57 pm

      Hi Marco,

      Actually those were not done on purpose, those were just simply typos. Good catch, I fat finger things sometimes. Glad it atleast got you going in the right direction.

      v/r
      James

  8. axiomkjt
    March 7, 2014 at 7:57 pm

    You’re missing a “/”+ in your expression. You have to have a proceeding “/” in the itemurl switch or it won’t work.

  9. axiomkjt
    March 7, 2014 at 8:16 pm

    Should be: @{Name=”itemURL”;Expression = {“/”+$_.RootFolder}})
    After I did that, the entire script worked like a charm. Thanks a lot it really saved me a lot of work. I needed to export 150 lists and libraries to a “swing” server to recover from a corruption.

    • March 10, 2014 at 2:50 pm

      Hi Axiomkjt,

      Glad you were able to get it to work. Interesting that you needed to include the “/” in the expression. In my case I did not need it, and my exports worked as my advertised script. This is a good note to those that my experience issues with the script I provided.

      Thanks
      JShidell

  1. April 30, 2012 at 2:32 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: