Download file from HTTP request – many times, due to the business requirement we might need to download a file from an external link through the HTTP or HTTPS API call. In this blog, we will learn about how to download a file from an HTTP request using a PowerShell script. Before getting into the script we must little know about Invoke-WebRequest.
What is Invoke-WebRequest in PowerShell HTTP call (invoke-webrequest PowerShell)?
In simple, using the Invoke-WebRequest PowerShell command we can download a file from an external link in a specific location.
How does Invoke-WebRequest command work in PowerShell (invoke-webrequest PowerShell)?
The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page, web service, or Web API. It parses the response and returns collections of links or a single link (based on the request being sent), images, and other HTML elements.
This cmdlet was introduced in PowerShell 3.0.
Beginning in PowerShell 7.0, Invoke-WebRequest supports proxy configuration defined by environment variables.
Example:
$Response = Invoke-WebRequest -URI https://www.bing.com/search?q=how+many+feet+in+a+mile $Response.InputFields | Where-Object { $_.name -like "* Value*" } | Select-Object Name, Value name value ---- ----- From Value 1 To Value 5280
The above uses the Invoke-WebRequest cmdlet to send a web request to the Bing.com site.
#################PowerShell script to download file from an external link##################################### cls $fileName = "Download_File_Report" #'yyyyMMddhhmm yyyyMMdd $enddate = (Get-Date).tostring("yyyyMMddhhmmss") $logFileName = $fileName +"_"+ $enddate+"_Log.txt" $invocation = (Get-Variable MyInvocation).Value $directoryPath = Split-Path $invocation.MyCommand.Path $directoryPathForLog=$directoryPath+"\"+"LogFiles" if(!(Test-Path -path $directoryPathForLog)) { New-Item -ItemType directory -Path $directoryPathForLog #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } #$logPath = $directoryPath + "\" + $logFileName $logPath = $directoryPathForLog + "\" + $logFileName $isLogFileCreated = $False function Write-Log([string]$logMsg) { if(!$isLogFileCreated){ Write-Host "Creating Log File..." if(!(Test-Path -path $directoryPath)) { Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } else { $script:isLogFileCreated = $True Write-Host "Log File ($logFileName) Created..." [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } else { [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } try { $folderWhereToDownload = "C:\temp" $externalFileURL= "https://ExternalLinkURLToDownload/fileName123.xlsx" $request = [System.Net.HttpWebRequest]::Create($externalFileURL) $request.Method = "HEAD" $response = $request.GetResponse() $fUri = $response.ResponseUri $fileName = [System.IO.Path]::GetFileName($fUri.LocalPath); $response.Close() $targetLocation = join-path $folderWhereToDownload $filename Invoke-WebRequest -Uri $externalFileURL -OutFile $targetLocation Write-Host "The file successfully has been downloaded" -ForegroundColor Green } Catch { $ErrorMessage = $_.Exception.Message +"in downloading file from an external link!:" Write-Host $ErrorMessage -BackgroundColor Red Write-Log $ErrorMessage } #################PowerShell script to download file from an external link - ends here##########################
How to use the above script (invoke-webrequest powershell)?
Just, we need to replace the below variables:
$folderWhereToDownload = "Local drive location to download the file" #Example: "C:\temp" $externalFileURL= "Here we need to pass the external file URL" #Example : "https://ExternalLinkURLToDownload/fileName123.xlsx"
Reference: Download file from HTTP request using PowerShell
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7#:~:text=%5B%5D-,Description,was%20introduced%20in%20PowerShell%203.0.
Summary: Download file from HTTP request using PowerShell
Thus in this blog, we have learned how to download a file from an HTTP external URL using the PowerShell script.
See Also: SharePoint PowerShell Tutorial
You may also like the following SharePoint PowerShell tutorials:
- Office 365: How to create content type in SharePoint Online using PowerShell?
- Office 365: How to create document library in SharePoint Online using PowerShell?
- Export SharePoint user information list to CSV(Excel) file using PowerShell
- How to fix “The term ‘Get-MsolUser’ is not recognized as the name of a cmdlet”
- How to fix the “The term ‘Get-SPWeb’ is not recognized as the name of a cmdlet, function” PowerShell error
- How to hide quick launch menu in SharePoint online using PnP PowerShell
- Edit user Permission is greyed Out SharePoint Online
- Get workflow inventory from SharePoint online using PowerShell CSOM
- Create a modern team site using PnP PowerShell in SharePoint
- In 2 steps convert a classic SharePoint page to modern using PnP
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- Create SharePoint online list using PnP provisioning template
- SharePoint Automation: PowerShell script to get remote server information
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM
- Invoke-WebRequest
