powershell script to download RRS feed

  • Question

  • $FilePath =  ""
    $localPath = "c:\test\ClientRep.xlsx"
    $wc = New-Object System.Net.Webclient
    $wc.UseDefaultCredentials = $true
    $wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    $wc.DownloadFile($FilePath, $localPath)

    I am able to download Excel from sharepoint site with above script but unable to open Excel file ,  getting error message "format or file type is invalid",  when i try to download manually from site it works fine but trying to do through script giving error.  

    do i need to specify any header information in above script



    Tuesday, January 3, 2017 3:19 AM

All replies

  • Hello,

    This is a forum for SQL Server Transact-SQL; you better should post your question to a PowerShell or SharePoint related forum.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Tuesday, January 3, 2017 6:15 AM
  • Hi,

    Please try to use SharePoint CSOM with PowerShell to achieve it, the following script for your reference:

    $siteUrl = ""
    $library = "Class Documents"
    $listItemId = 24 #item's list ID
    $toPath = "c:\xfer\" #yourfile.xlsx"
    $domain="doamin name"
    #$username = ""
    #$password = Read-Host -Prompt "Password for $username" -AsSecureString 
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $context.Credentials=New-Object System.Net.NetworkCredential($username, $password, $domain);
    #$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$password) 
    $web = $context.Web
    $list = $context.Web.Lists.GetByTitle($library)
    $listitem = $list.GetItemById($listItemId)
    $fileRef = $file.ServerRelativeUrl
    $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($context, $fileRef)
    $fileName = $toPath + $File.Name
    $fileStream = [System.IO.File]::Create($fileName)

    More information:

    CSOM SharePoint PowerShell Reference and Example Codes

    Download a document from  SharePoint Document Library using CSOM 2013

    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Wednesday, January 4, 2017 6:58 AM