The following PowerShell can be used to take a subset of files from a document library in SharePoint and output them to the file system. Note, this will need to be executed on the SharePoint server.
[code language=”PowerShell”]
$spweb = get-spweb -identity "http://yourdomain/yourweb"
# Use the "pretty name" of your library below
$list = $spweb.lists["Your Library"]
# Below is an example of matching an attribute (Type) with a wildcard (*Queen*)
$listItemsLikeQueen = $list.Items | ?{$_["Type"] -like "*Queen*"}
# Below is an example of matching based on a Content Type
$listItems = $listItemsLikeQueen | ?{$_.ContentType.Name -eq "Monarchy"}
$Destination = "D:\Out"
# Use the name of your library as you named it at the time of creation below (we all remove spaces when we create libraries, right?)
$DocumentLibrary = "YourLibraryWithoutSpaces"
# For each item, output it to the destination path above
foreach ($listItem in $listItems)
{
# Change the output path of the list item, and swap around the slashes
$DestinationPath = $listItem.Url.replace("$DocumentLibrary","$Destination").Replace("/","")
write-host "Downloading $($listItem.Name) -> $DestinationPath"
if (!(Test-Path -path $(Split-Path $DestinationPath -parent)))
{
write-host "Creating $(Split-Path $DestinationPath -parent)"
$dest = New-Item $(Split-Path $DestinationPath -parent) -type directory
}
$binary = $spWeb.GetFile($listItem.Url).OpenBinary()
$stream = New-Object System.IO.FileStream($DestinationPath), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
#Clean up
$spWeb.Dispose()
[/code]
The inspiration for this came from the following forum post
As well as this blog post (the blog post below shows you how to take ALL the files in a library and output them to a local path).
And some ideas of how to filter items in the list using PowerShell can be found below.
It’s not working for me. The script is not giving any erros, but its not downloading any files in the location I specified.
I think I don’t understand the ‘# Change the output path of the list item, and swap around the slashes’ part. What should I do there? I already specified the document library and the location where do download to. So what should I do here then?
Figured it out already. Had to change this:
foreach ($listItem in $listItems)
to this:
foreach ($listItem in $listItems Like Queen)
to apply the filter.
Thanks anyway, your script was a great help!