Powershell Week – Day 4: Copying Files Between Document Libraries


Day 4 of the Powershell Week is all about copying files from one document library to another.


# Source document library full path
$source = "http://yourwebapplication.com/web/sourcedoclib"
# Destination document library full path
$destination = "http://yourwebapplication.com/web/destinationdoclib"

$sourceList = Get-SPList $source
$sourceFieldCol = $sourceList.Fields;
$sourceItems = $sourceList.GetItems();

$destinationList = Get-SPList $destination

foreach($spListItem in $sourceItems) {
 $newSPListItem = $destinationList.RootFolder.Files.Add($spListItem.Name, $spListItem.File.OpenBinary()).Item;;

foreach($spField in $sourceFieldCol) {
 if ($spField.ReadOnlyField -ne $True) {
 $newSPListItem[$($spField.InternalName)] = $spListItem[$($spField.InternalName)];
 }
 }
 $newSPListItem.Update();
}

Get the script here.
See you,

Amadeu.

Advertisements

2 Responses to Powershell Week – Day 4: Copying Files Between Document Libraries

  1. chiqnlips says:

    Great post! Where are you defining your Get-SPList function?

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: