Using PowerShell, I want to download hundreds of 1GB files from Microsoft 365 SharePoint…
- User
Using PowerShell, I want to download hundreds of 1GB files from Microsoft 365 SharePoint Online.
Assistant
You can use the SharePoint Online PowerShell module to download files from SharePoint Online. Here is an example of how you can download a file from a SharePoint Online site:
# Import the SharePoint Online Management Shell module Import-Module SharePointPnPPowerShellOnline # Connect to SharePoint Online Connect-PnPOnline -Url <site URL> -Credentials (Get-Credential) # Set the file path and name $filePath = "C:\download\file.txt" $fileName = "file.txt" # Download the file Get-PnPFile -Url <file URL> -Path $filePathNote that this will only download one file at a time. If you have hundreds of files to download, you will need to loop through a list of file URLs and use the
Get-PnPFilecommand in a loop. This can take a long time, so you may want to consider using a tool that can download files in parallel, such as PowerShell'sStart-Jobcommand.Also, keep in mind that downloading large files can consume a lot of network bandwidth and may affect the performance of the SharePoint Online site. You should perform these operations during off-peak hours if possible, and make sure that you have sufficient network bandwidth to handle the downloads.
- User
I don't have a list of file names. I want to download every file in the folder I specify. I think 3 parallel download threads would work well for my resources. I'm using the newest PowerShell and have all the modules installed.
Assistant
You can use the SharePoint Online PowerShell module to download all the files in a folder from SharePoint Online. Here's an example of how you can download all the files in a folder using 3 parallel download threads:
# Import the SharePoint Online Management Shell module Import-Module SharePointPnPPowerShellOnline # Connect to SharePoint Online Connect-PnPOnline -Url <site URL> -Credentials (Get-Credential) # Set the folder path $folderPath = "/sites/<site name>/<library name>/<folder name>" # Get all the files in the folder $files = Get-PnPFolderItem -FolderSiteRelativeUrl $folderPath | Where-Object { $_.File.ServerRelativeUrl -ne $null } # Define a function to download a file function Download-File { param([string]$filePath, [string]$fileName) $fullFilePath = "$filePath\$fileName" Get-PnPFile -Url $fileName -Path $fullFilePath } # Create a list to store the file paths and names $filePathsAndNames = @() # Loop through the files and add the file paths and names to the list foreach ($file in $files) { $filePathsAndNames += @{ FilePath = "C:\download\" FileName = $file.Name } } # Create an array of jobs $jobs = 1..3 | ForEach-Object { Start-Job -ScriptBlock { foreach ($file in $using:filePathsAndNames) { Download-File -FilePath $file.FilePath -FileName $file.FileName } } } # Wait for all the jobs to complete Wait-Job -Job $jobs # Get the results of the jobs $results = Receive-Job -Job $jobs # Display the results Write-Output $resultsThis script will download all the files in the specified folder using 3 parallel download threads. You can adjust the number of threads by changing the value of the
jobsvariable.Keep in mind that downloading large files can consume a lot of network bandwidth and may affect the performance of the SharePoint Online site. You should perform these operations during off-peak hours if possible, and make sure that you have sufficient network bandwidth to handle the downloads.
- User
Will the downloaded file names be preserved? I need them to stay the same.
Assistant
Yes, the downloaded file names will be preserved. In the example script I provided, the
Get-PnPFilecommand is used with the-Pathparameter to specify the output file path, and the-Urlparameter to specify the file URL. The-Urlparameter is set to the file name, which is retrieved from theNameproperty of theFileobject. This will ensure that the file names are preserved when the files are downloaded.For example, if the file name in the SharePoint Online library is "file.txt", the downloaded file will also be named "file.txt".
It's worth noting that if there are duplicate file names in the folder, the downloaded files will also have duplicate file names. In this case, you may want to add a timestamp or some other identifier to the file names to avoid conflicts.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.