title: How to use PowerShell to download files from SharePoint?
body: <p>I've used the following sites to help me get this far and to troubleshoot.</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/26928822/download-file-from-sharepoint">Download file from SharePoint</a></li>
  <li><a href="https://stackoverflow.com/questions/28946557/how-to-download-newest-file-from-sharepoint-using-powershell">How to download newest file from SharePoint using PowerShell</a></li>
  <li><a href="http://techtrainingnotes.blogspot.com/2014/02/download-file-from-sharepoint-using.html" rel="nofollow noreferrer">Mike Smith's Tech Training Notes SharePoint, PowerShell and .Net!</a></li>
  <li><a href="https://gist.github.com/mdnmdn/1493541" rel="nofollow noreferrer">Upload file to a SharePoint doc library via PowerShell </a></li>
  <li><a href="https://stackoverflow.com/questions/33791155/download-latest-file-from-sharepoint-document-library">Download latest file from SharePoint Document Library</a></li>
  <li><a href="https://sharepoint.stackexchange.com/questions/65114/how-to-iterate-each-folders-in-each-of-the-sharepoint-websites-using-powershell">How to iterate each folders in each of the SharePoint websites using PowerShell</a></li>
  <li><a href="https://sharepoint.stackexchange.com/questions/200903/powershells-get-childitem-on-sharepoint-library">PowerShell's Get-ChildItem on SharePoint Library</a></li>
  </ol>
  
  <p>I am trying to download random files from SharePoint folder, and I have it working for when I actually know the file name and extension. </p>
  
  <p>Working code with name and extension:</p>
  
  <pre><code>$SharePoint = "https://Share.MyCompany.com/MyCustomer/WorkLoad.docx"
  $Path = "$ScriptPath\$($CustomerName)_WorkLoad.docx"
  
  #Get User Information
  $user = Read-Host "Enter your username"
  $username = "$user@MyCompany"
  $password = Read-Host "Enter your password" -AsSecureString
  
  #Download Files
  $WebClient = New-Object System.Net.WebClient
  $WebClient.Credentials = New-Object System.Net.Networkcredential($UserName, $Password)
  $WebClient.DownloadFile($SharePoint, $Path)
  </code></pre>
  
  <p>However, I don't seem to be able to figure out how to do it with multiple files of unknown names or extensions.</p>
  
  <p>I have tried mapping a drive, only to end up with "drive mapping failed" &amp; "The network path was not found." errors:</p>
  
  <pre><code>$SharePoint  = Read-Host 'Enter the full path to Delivery Site'
  $LocalDrive  = 'P:'
  $Credentials = Get-Credential
  
  
  if (!(Test-Path $LocalDrive -PathType Container)) {
      $retrycount = 0; $completed = $false
      while (-not $completed) {
          Try {
              if (!(Test-Path $LocalDrive -PathType Container)) {
                  (New-Object -ComObject WScript.Network).MapNetworkDrive($LocalDrive,$SharePoint,$false,$($Credentials.username),$($Credentials.GetNetworkCredential().password))
              }
              $Completed = $true
          }
          Catch {
              if ($retrycount -ge '5') {
                  Write-Verbose "Mapping SharePoint drive failed the maximum number of times"
                  throw "SharePoint drive mapping failed for '$($SharePoint)': $($Global:Error[0].Exception.Message)"
              } else {
                  Write-Verbose "Mapping SharePoint drive failed, retrying in 5 seconds."
                  Start-Sleep '5'
                  $retrycount++
              }
          }
      }
  }
  </code></pre>
  
  <p>I've also used the following code with similar results or no results at all.</p>
  
  <pre><code>#Get User Information
  $user = Read-Host "Enter your username"
  $username = "$user@MyCompany"
  $password = Read-Host "Enter your password" -AsSecureString
  
  #Gathering the location of the Card Formats and Destination folder
  $Customer = "$SharePoint\MyCustomer"
  $Products = "$Path\$($CustomerName)\Products\"
  
  #Get Documents from SharePoint
  $credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
  New-PSDrive -Credential $credential -Name "A" -PSProvider "FileSystem" -Root "$SharePoint"
  net use $spPath #$password /USER:$user@corporate
  
  #Get PMDeliverables file objects recursively
  Get-ChildItem -Path "$Customer" | Where-Object { $_.name -like 'MS*' } | Copy-Item -Destination $Products  -Force -Verbose
  </code></pre>