SharePoint 2010 and PowerShell: How to copy files from document library to another document library in the same site collection?

Scenario

We want to copy item to another document library for archive or some other purpose like added extra column/information for searching, etc.

We want to get this achieved by using PowerShell.

Solution

$fromWeb = Get-SPWeb “http://….”
$libFile = $fromWeb.GetFile(“http://…/item.docx”)
$folder = $web.GetFolder(“Target Document Library”)
#Copy the file into the document library using file stream
$f = $folder.Files.Add($File.FileName, $libFile.OpenBinary(), $overwrite)
#Copy across the existing data
$item = $f.Item
$item[“Modified”] = $libFile.TimeLastModified.ToLocalTime()
$item[“Created”] = $libFile.TimeCreated.ToLocalTime()
$item[“Author”] = $libFile.Author
$item[“Modified By”] = $libFile.ModifiedBy

#To overwrite the property
$item.UpdateOverwriteVersion()
$fromWeb.Dispose()

 

Advertisements

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