How to use PowerShell to download files from SharePoint?

被刻印的时光 ゝ 提交于 2019-12-05 16:30:21

Without defined "input parameters", it's not exactly clear the full solution you need so I'll provide a few snippets of PowerShell that should be of use based on what you've described.

I'll spare you the basics of the various OOTB functions (i.e. Get-SPWeb, etc) though can provide those details as well if needed. I've also been overly explicit in the scripting, though know some of these lines could be chained, piped, etc to be made shorter & more efficient.

This example will iterate over the contents of a SharePoint Library and download them to your local machine:

$Destination = "C:\YourDestinationFolder\ForFilesFromSP"
$Web = Get-SPWeb "https://YourServerRoot/sites/YourSiteCollection/YourSPWebURL"
$DocLib = $Web.Lists["Your Doc Library Name"]
$DocLibItems = $DocLib.Items

foreach ($DocLibItem in $DocLibItems) {
    if($DocLibItem.Url -Like "*.docx") {
        $File = $Web.GetFile($DocLibItem.Url)
        $Binary = $File.OpenBinary()
        $Stream = New-Object System.IO.FileStream($Destination + "\" + $File.Name), Create
        $Writer = New-Object System.IO.BinaryWriter($Stream)
        $Writer.write($Binary)
        $Writer.Close()
    }
}

This is pretty basic; the variables up top are where on your local machine you wish to store the download files ($Destination), the URL of your SharePoint Site/Web ($Web) and the name of the Document Library (Your Doc Library Name).

The script then iterates through the items in the Library (foreach ($DocLibItem in $DocLibItems) {}), optionally filters for say items with a .docx file extension and downloads each to your local machine.

You could customize this further by targeting a specific sub-folder within the Doc Library, filter by metadata or properties of the Docs or even iterate over multiple Sites, Webs and/or Libraries in one script, optionally filtering those based on similar properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!