How to Download A File From Sharepoint Using Powershell?

11 minutes read

To download a file from SharePoint using PowerShell, you can use the SharePoint Online Management Shell or the SharePoint PnP PowerShell module. First, connect to your SharePoint site using the Connect-SPOService cmdlet. Next, use the Get-PnPFile command to retrieve the file you want to download. Finally, use the Save-PnPFile cmdlet to save the file to your local machine. Make sure you have the necessary permissions to access the file in SharePoint before downloading it.

Best Powershell Books to Read in December 2024

1
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

Rating is 5 out of 5

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

2
PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

Rating is 4.9 out of 5

PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

3
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Rating is 4.8 out of 5

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

4
Mastering PowerShell Scripting: Automate and manage your environment using PowerShell 7.1, 4th Edition

Rating is 4.7 out of 5

Mastering PowerShell Scripting: Automate and manage your environment using PowerShell 7.1, 4th Edition

5
Windows PowerShell in Action

Rating is 4.6 out of 5

Windows PowerShell in Action

6
Learn PowerShell Scripting in a Month of Lunches

Rating is 4.5 out of 5

Learn PowerShell Scripting in a Month of Lunches

7
Windows PowerShell Step by Step

Rating is 4.4 out of 5

Windows PowerShell Step by Step

8
PowerShell Pocket Reference: Portable Help for PowerShell Scripters

Rating is 4.3 out of 5

PowerShell Pocket Reference: Portable Help for PowerShell Scripters


How to download files from SharePoint using SharePoint Designer workflow actions in PowerShell?

To download files from SharePoint using SharePoint Designer workflow actions in PowerShell, you can follow these steps:

  1. Open SharePoint Designer and create a new workflow.
  2. Add a "Call HTTP web service" action to your workflow.
  3. Configure the action to make a GET request to the URL of the file you want to download.
  4. Add a "Build Dictionary" action to the workflow.
  5. Add a key-value pair to the dictionary with the key "Accept" and the value "application/octet-stream".
  6. Add a key-value pair to the dictionary with the key "Authorization" and the value "Bearer {your access token}".
  7. Add the dictionary to the "RequestHeaders" parameter of the "Call HTTP web service" action.
  8. Add a "Send HTTP web service" action to the workflow.
  9. Configure the action to store the response content in a variable.
  10. Use PowerShell to read the response content and save it to a file on your local machine.


Here is an example PowerShell script that demonstrates how to download a file from SharePoint using this approach:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$siteUrl = "https://yoursharepointsite.com"
$webUrl = "https://yoursharepointsite.com/subsite"
$filePath = "/sites/yourdocumentlibrary/yourfile.txt"

# Get the access token
$accessToken = Get-SPOToken -SiteUrl $siteUrl

# Make a GET request to download the file
$uri = "$webUrl/_api/web/GetFileByServerRelativePath(decodedurl='$filePath')/\$value"
$response = Invoke-RestMethod -Uri $uri -Headers @{ "Accept" = "application/octet-stream"; "Authorization" = "Bearer $accessToken" }

# Save the file to your local machine
$localPath = "C:\Users\yourusername\Downloads\yourfile.txt"
[System.IO.File]::WriteAllBytes($localPath, $response)


In this script, the Get-SPOToken function is used to get the access token for authentication. You can implement this function using the SharePoint Online PowerShell module or any other method that provides the access token.


Replace the siteUrl, webUrl, filePath, and localPath variables with the appropriate values for your SharePoint site, file, and local machine.


Run the PowerShell script to download the file from SharePoint to your local machine.


How to connect to a SharePoint Online site using PowerShell?

To connect to a SharePoint Online site using PowerShell, you can follow these steps:

  1. Open PowerShell as an administrator on your computer.
  2. Install the SharePoint Online Management Shell module by running the following command:
1
Install-Module -Name Microsoft.Online.SharePoint.PowerShell


  1. After the module is installed, you can connect to your SharePoint Online site by running the following command and entering your SharePoint Online site URL and credentials when prompted:
1
Connect-SPOService -Url <Your SharePoint Online site URL>


  1. Once you are successfully connected, you can run PowerShell cmdlets to manage your SharePoint Online site, such as getting site collections, lists, and items.
  2. To disconnect from the SharePoint Online site, you can run the following command:
1
Disconnect-SPOService


By following these steps, you can easily connect to a SharePoint Online site using PowerShell and manage it through PowerShell cmdlets.


How to download files from a SharePoint document library with unique permissions using PowerShell?

To download files from a SharePoint document library with unique permissions using PowerShell, you can use the following script:

  1. First, you will need to authenticate against the SharePoint site using the following command:
1
2
$cred = Get-Credential
Connect-PnPOnline -Url https://yoursite.sharepoint.com -Credentials $cred


  1. Once authenticated, you can use the following script to download files from the document library with unique permissions:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$libraryName = "Documents"
$localFolder = "C:\Downloads"

$files = Get-PnPListItem -List $libraryName

foreach($file in $files){
    $fileName = $file.FieldValues["FileLeafRef"]
    $fileUrl = $file.FieldValues["FileRef"]

    $fileStream = Get-PnPFile -Url $fileUrl -FileName $fileName -AsFile -Path $localFolder
}


Replace "Documents" with the name of your document library and "C:\Downloads" with the local folder where you want to save the downloaded files.

  1. Run the script in PowerShell to download files from the SharePoint document library with unique permissions.


Note: Make sure you have the SharePoint Online Management Shell module installed on your machine before running the script.


What is SharePoint Online Management Shell and how to use it for downloading files?

SharePoint Online Management Shell is a set of Windows PowerShell cmdlets that can be used to manage SharePoint Online within the Office 365 environment. It allows administrators to perform various tasks such as managing site collections, lists, libraries, users, and more.


To use SharePoint Online Management Shell for downloading files, you can follow these steps:

  1. Launch SharePoint Online Management Shell: Open the SharePoint Online Management Shell application on your computer.
  2. Connect to SharePoint Online: Use the Connect-SPOService cmdlet to connect to your SharePoint Online site. You will need to enter your SharePoint Online admin URL and credentials to establish a connection.
  3. Navigate to the SharePoint document library: Use the Get-SPOFolder cmdlet to navigate to the document library where the file is located. You may need to specify the site URL and library name in the cmdlet.
  4. Download the file: Use the Get-SPOFile cmdlet to download the file from the document library. You will need to specify the file name and destination path where you want to save the file on your local computer.


Example:

1
2
3
Connect-SPOService -Url https://yourtenant-admin.sharepoint.com
$folder = Get-SPOFolder -SiteRelativeUrl "/sites/your-site" -LibraryName "Documents"
Get-SPOFile -ServerRelativeUrl $folder.ServerRelativeUrl + "/file.pdf" -LocalFile "C:\Downloads\file.pdf"


In the example above, replace "https://yourtenant-admin.sharepoint.com" with your SharePoint Online admin URL, "/sites/your-site" with the site URL where the document library is located, "Documents" with the library name, "/file.pdf" with the file path within the library, and "C:\Downloads\file.pdf" with the local destination path.


By following these steps, you can use SharePoint Online Management Shell to download files from your SharePoint Online environment.


How to download files from a SharePoint subsite using PowerShell?

To download files from a SharePoint subsite using PowerShell, you can use the following script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for SharePoint Online Site URL, Username and Password
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
$username = "[email protected]"
$password = "yourpassword"

#Setup the context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, (ConvertTo-SecureString $password -AsPlainText -Force))

#Get the subsite
$subSiteUrl = "/subsite"
$subSite = $ctx.Web.GetSubwebForUrl($subSiteUrl)
$ctx.Load($subSite)
$ctx.ExecuteQuery()

#Get the files from the subsite
$list = $subSite.Lists.GetByTitle("Documents")
$ctx.Load($list)
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($query)
$ctx.Load($items)
$ctx.ExecuteQuery()

#Download each file
foreach ($item in $items) {
    $file = $item.File
    $ctx.Load($file)
    $ctx.ExecuteQuery()

    $fileUrl = $file.ServerRelativeUrl
    $downLoadUrl = $siteUrl + $fileUrl
    $fileDest = "C:\temp\" + $file.Name

    $fileInfo = [System.IO.File]::Create($fileDest)
    $fileInfo.Close()
    $fileInfo.Dispose()

    Invoke-WebRequest -Uri $downLoadUrl -OutFile $fileDest -Credential $ctx.Credentials
}

$ctx.Dispose()


Make sure to replace the placeholders with your actual SharePoint Online site URL, username, and password. This script will connect to the SharePoint site, find the subsite, get the list of documents, and download each file to your local machine.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To use PowerShell to download files from SharePoint, you can first connect to the SharePoint site using the SharePoint Online Management Shell. Once connected, you can use the Get-PnPFile command to retrieve files from a document library in SharePoint. You wil...
To use PowerShell to set some primitive files, you can start by opening PowerShell on your computer. You can do this by searching for PowerShell in the Start menu or by pressing Windows + R, typing &#34;powershell&#34; and pressing Enter.Once PowerShell is ope...
To download a file in Laravel, you can use the response()-&gt;download() method in your controller. First, you need to specify the path of the file you want to download. You can then return a response with the download method, passing in the file path as the f...