2

I have created a WorkFlow to create a document populating it with data from a list. I now need a powershell script that can copy the file in the document library to a file share on our server.

I am new to using powershell in SharePoint, is this possible with a script?

2
  • yes it is possible Commented Mar 1, 2012 at 12:18
  • @SebastienStettler I did find a post on this site sharepoint.stackexchange.com/questions/6511/… It included a script which I have altered which copies files however how would I change it to only fetch a single file? Commented Mar 1, 2012 at 13:33

1 Answer 1

3

You can use this method.

$fileUrl = url for file on sharepoint

$destinationfolder = destinationFolder


    function ProcessURL {
        param($fileUrl,$destinationfolder)
        $pathToTry = "http://www.mysharepointsite.com/somesite"
        $site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $pathToTry
        $web = $site.OpenWeb()  
        $file = $web.GetFile($fileUrl)
        $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)
        $writer.write($binary)
        $writer.Close()
        #Delete file by deleting parent SPListItem
        $list.Items.DeleteItemById($file.Item.Id)
        $web.Close()
        $site.Close()
    }
5
  • Where are we passing Username and Password? or How do we pass Username and Password? Commented Mar 8, 2018 at 17:52
  • @AXA240778 You shouldn't need a username/password. This should be ran on the SharePoint server while logged in with an account that has the necessary privileges and you shouldn't get prompted. If you are, you may need to check your privileges. Commented Apr 2, 2019 at 17:59
  • @AXA240778 One caveat: I noticed if you just specify the URL directly & try to go to it directly without properly setting up your objects, I could see how you might get prompted. This happens on an HttpRequest, for example, if you don't include CredentialsCache.DefaultNetworkCredentials. SPSite & SPWeb must be appropriately defined, above. It should be: $pathToTry = "http://mysharepointsite.com/somesite" $site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $pathToTry $web = $site.OpenWeb() and then you need to close them at the end: $web.Close() $site.Close() Commented Apr 4, 2019 at 17:57
  • @AXA240778 Added an edit to the answer to make it functional, with those lines, to demonstrate. Commented Apr 4, 2019 at 18:06
  • 1
    Thansk much @vapcguy Commented Apr 4, 2019 at 18:12

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.