I'm looking to build a Powershell script that does the following to all documents in a SharePoint 2010 document library:

  1. Downloads the document to a network file share
  2. Deletes the original document in the document library

I started experimenting with the following:

$Url = "http://sharepoint2010/myweb/Shared%20Documents/aaa.txt"
$Path = "C:\test\aaa.txt"
$Username = ""
$Password = ""

$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $url, $path )

However I get "(401) Unauthorized" exceptions even if I execute this on the SharePoint server. Note that this script will execute on a schedule running on a SharePoint WFE server.

  • I am getting error: New-Object : Exception calling ".ctor" with "2" argument(s): "Could not find a part of the path and sometimes it does not copy anything but deletes the folders and files Can someone please suggest?
    – user51776
    Aug 23, 2018 at 16:28

3 Answers 3


I think that if you planing to create a timer job then better investigate how to do it using c# instead of powershell and when the code is running on a timer job it will use the farm account so you wont need to impersonate any account.

Here is a good example of moving/copying documents using the SharePoint object model http://geek.hubkey.com/2007/12/move-sharepoint-document-library-files.html

Hope this helps

  • Hey Renzo, I agree that doing it by code would be a good approach but I need do achieve this through a script. By timer job I didn't mean SharePoint time job, just a scheduled task through Windows. Thanks for your response though!
    – Jonny
    Oct 25, 2010 at 12:23
  • Hi Jonny, Fair enough :) How about using the unc path instead ? and then you can use the move-item command-let for example to move a file should be as simple as Move-Item "\\testserver\DavWWWRoot\Shared Documents\test.txt" "c:\" This will move the test.txt to your c drive you will need to ensure that your schedule task "run as" its set to a user with access to SharePoint and to the folder where you are saving you files
    – Renzo
    Oct 25, 2010 at 15:30
  • Great suggestion, I'm testing that out and will let you know if it works...
    – Jonny
    Oct 25, 2010 at 16:20

Since I don't see PowerShell script that downloads and deletes all files from document library to local or network drive in any of the answers I will post one (this script also keeps folder structure on destination drive):

Add-PSSnapin Microsoft.SharePoint.PowerShell

$destination = "C:\\test\\"
$web = Get-SPWeb -Identity "http://sharepoint2010/myweb/"
$list = $web.GetList("http://sharepoint2010/myweb/Shared%20Documents/")

function ProcessFolder {
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
            $dest = New-Item $destinationfolder -type directory 
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        #Delete file by deleting parent SPListItem

#Download root files
#Download files in folders
foreach ($folder in $list.Folders) {

#Delete folders
foreach ($folder in $list.Folders) {
    try {
    catch {
        #Deletion of parent folder already deleted this folder
        #I really hate this

Permission issues? Just run it on SharePoint server as admin and it will work :)

  • 3
    Permission issues? Resolve them by applying the correct permissions to the appropriate folders and sites. Never recommend a "just run as admin" mindset to a production environment. Good script, though.
    – James Love
    Jul 22, 2011 at 23:00
  • James, I totally agree with you about permissions. I was a bit annoyed with other answers elaborating about permissions and not about script that was in original question. Jul 23, 2011 at 0:39
  • Arguably, the question was he was getting a 401, so that does relate to perms, but a full, usable script block is a valuable answer.
    – James Love
    Jul 23, 2011 at 9:23

If you're running this impersonating the farm account, you won't (by default) have access to the content of sites.

you will have to impersonate an account which has access to that site, but then you have the issue of storing credentials in a script, which is very ill-advised (particularly if it's farm account credentials you have stored in there).

Is there any particular reason why you need this ran as a script?

  • Hello, the motivation to do this by script is for simple management and scheduling. There are also budget constraints associated with developing custom code. If this can't easily be achieved through powershell then I'll have to consider alternatives...
    – Jonny
    Oct 25, 2010 at 13:05
  • The scheduling and management (enable/disabling) for Timer Jobs can easily be set up in a secure environment within Central Administration. With your business case for development of custom code, I'd suggest presenting the risk of having the entire SharePoint configuration username and password stored in a plain text file against having it locked away within the (tried and tested) security of SharePoint Central Administration.
    – James Love
    Oct 25, 2010 at 13:45
  • If the task is running through the context of the Windows Task Scheduler, would it not pass on those credentials and not require the username/password to be embedded into the script?
    – Jonny
    Oct 25, 2010 at 14:44
  • Yep, you'd probably need not create a credential object like your sample code above, and Renzo's suggestion may work even better (he sits behind me in the office, I know his PowerShell is good :) )
    – James Love
    Oct 25, 2010 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.