Copy Files with version and metadata using PowerShell

Introduction

This script can copy a List / Library from one web to another web while maintaining versions and metadata.

Scenarios

In a real world, a lot of people ask copying just one List / Library from one web to another web while retaining versions, security info and metadata; this script would help to facilitate that.

Script

This script contains one single PS1 script:
You can use this script in the following way:
    Open SharePoint 2010 Management Shell with a proper account that has sufficient permission on both source and destination web.
PowerShell
Edit|Remove
if ( (Get-PSSnapin-Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
{Add-PSSnapin Microsoft.SharePoint.Powershell} 
  
#$SourceSite = "http://intranet.contoso.com/"#$ItemURL = "/Lists/Tasks"#$DestSite = "http://www.cotoso.com/"$Path = $ENV:HOMEDRIVE + "\" 
$Filename = $Path+"export.cmp"$SourceSite = Read-Host"Enter Source Site URL"$ItemURL = Read-Host"Enter relative URL of List / Library e.g. /Lists/Tasks or /Shared Documents"$DestSite = Read-Host"Enter Destination Site URL" 
Write-Host "Exporting from Source `t`t: "-NoNewline; Write-Host $SourceSite$ItemURL-ForegroundColor "Magenta"; Write-Host "Importing to Destination `t: "-NoNewline; Write-Host $DestSite$ItemURL-ForegroundColor "Magenta" 
Write-Host "The Export file and import / export logs will be available at "-NoNewline; Write-Host $Path-ForegroundColor "Magenta" 
Write-Host "Note :"  
Write-Host "1) Destination Library will be created if it doesnt exist. If exists then items will be added to it" 
Write-Host "2) If the item(s) exists, they will NOT be overwritten, instead a new item will be added" 
  
Export-SPWeb -Identity $SourceSite-Path $Filename-ItemUrl $ItemURL-IncludeUserSecurity -IncludeVersions 4 -Force 
Import-SPWeb -Identity $DestSite-Path $Filename-ActivateSolutions -IncludeUserSecurity 
Write-Host `t$Filename".export.log"

Examples

Example 1: How to export and import “Tasks” list from one web to another web
Execute the script and it will ask for the required details.
 

Example 2: How to export and import “Shared Documents” library from one web to another web
Execute the script and it will ask for the required details.