To download the file from the URL in PowerShell, use the Invoke-WebRequest cmdlet. The Invoke-WebRequest command sends HTTP, HTTPS, and FTP requests to a web page or service and parses the response. This command returns the collections of links, images, etc…
In this article, we will cover
- Step-by-step guide on PowerShell download the zip file from the URL.
- PowerShell script to download files from URL.
- PowerShell script to download the zip file from the URL and extract it.
This blog post will provide step by step-by-step guide to download files from a URL using the Invoke-WebRequest cmdlet and how to download the zip file from url and extract it to the specified location.
How to Download Zip File from URL using PowerShell
The following steps describe the PowerShell script to download the zip file from the internet and unzip it.
Assign the Download zip file from the URL path to the variable
- Open Windows PowerShell ISE
- Go to File and click new
- In the example, we will use GitHub repository zip file URL for our example.
$Url = "https://github.com/microsoft/vscode-docker/archive/master.zip"
The above command will assign the GitHub repository master.zip URL path to the $Url variable.
Get file name along with extension from URL using Split-Path cmdlet
$DownloadZipFile = "D:\PowerShell\Zip\ZipFile\" + $(Split-Path -Path $Url -Leaf)
The above command will return the filename along with the extension eg.. master.zip and assign it to $DownloadZipFile
variable.
PowerShell Tip: Download the PSexec utility zip file for PSRemoting in PowerShell!
Set destination folder path for unzip files
$ExtractPath = "D:\PowerShell\Zip\UnZipFiles\"
Use Invoke-WebRequest
cmdlet for the above URL to save the response body and create a shell object
Invoke-WebRequest -Uri $Url -OutFile $DownloadZipFile $ExtractShell = New-Object -ComObject Shell.Application
PowerShell extracts zip file items
$ExtractFiles = $ExtractShell.Namespace($DownloadZipFile).Items()
Use ExtractFiles to copy the extracted file to the destination folder and start a process
$ExtractShell.NameSpace($ExtractPath).CopyHere($ExtractFiles) Start-Process $ExtractPath
The above guide will help you to understand the step-by-step procedure for using a PowerShell script to download the zip file from url and unzip the file to the destination folder.
Copy the below Powershell download file from url script in ISE and run it. Make sure to give the DownloadZipFile path and ExtractFilePath correctly, otherwise, it may throw an error if a folder or drive is not available in your system.
#github url to download zip file #Assign zip file url to local variable $Url = "https://github.com/microsoft/vscode-docker/archive/master.zip" $DownloadZipFile = "D:\PowerShell\Zip\ZipFile\" + $(Split-Path -Path $Url -Leaf) $ExtractPath = "D:\PowerShell\Zip\UnZipFiles\" Invoke-WebRequest -Uri $Url -OutFile $DownloadZipFile $ExtractShell = New-Object -ComObject Shell.Application $ExtractFiles = $ExtractShell.Namespace($DownloadZipFile).Items() $ExtractShell.NameSpace($ExtractPath).CopyHere($ExtractFiles) Start-Process $ExtractPath
Read more about how to unzip a file to a folder using PowerShell.
How to Download a File from URL with PowerShell
To download a file from the URL, use the Invoke-RestMethod cmdlet in PowerShell which takes the source file URL and downloads the file to the destination location.
For example, let’s consider an example of downloading a file from the GitHub repository.
File name: powershell-beginners-guide.md
Destination to download a file from URL : D:\
$source = 'https://github.com/PowerShell/PowerShell/blob/master/docs/learning-powershell/powershell-beginners-guide.md' $destination = 'D:\powershell-beginners-guide.md' Invoke-RestMethod -Uri $source -OutFile $destination
In the above PowerShell script, the $source
variable contains the download file URL path.
The $destination
variable stores the location to download a zip file on a local computer.
Using Invoke-RestMethod
, it downloads a file from the specified URL and saves it to the location.
PowerShell Download File using Invoke-RestMethod
In PowerShell, downloading files from a URL using the Invoke-RestMethod cmdlet is similar to Invoke-WebRequest and uses the same parameters like source URL and destination to copy a zip file on the drive.
Let’s consider an example, download the Machine Learning beginner course material zip file from url given below.
https://github.com/microsoft/ML-For-Beginners/archive/refs/heads/main.zip
Use below PowerShell script below to download a file from the https URL.
$source = 'https://github.com/microsoft/ML-For-Beginners/archive/refs/heads/main.zip' $destination = 'D:\Temp\MLCourse\main.zip' Invoke-RestMethod -Uri $source -OutFile $destination
In the above example of PowerShell, the download file from URL,
$source
variable contains the GitHub main.zip file path.$destination
variable contains the folder path to the zip file.- Using the PowerShell Invoke-RestMethod cmdlet, use $source and $destination parameters to download a file from url in PowerShell.
Cool Tip: How to use Stop-Process to kill process in PowerShell!
Conclusion
We have covered the steps to download a file from url, download the zip file, and extract the zip file to the destination folder.
Using PowerShell Invoke-WebRequest to download a file from url and extract the zip file to the folder on the destination location.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.