PowerShell Download File from URL

Overview

  • Step by Step guide on PowerShell download file from url
  • PowerShell script to download file from url
  • PowerShell script to download zip file from url and extract it

Have you ever come across requirement using PowerShell script to download file from url and unzip compressed files?

This blog post will provide step by step guide on download file from url using Invoke-WebRequest cmdlet and how to download zip file from url and extract it to specified location.

PowerShell Download File from Url

In PowerShell to download file from url, use Invoke-RestMethod cmdlet which takes source file url and download file to destination location.

For example, let’s consider an example to download file from github repository.

File name : powershell-beginners-guide.md

Destination to download 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, $Source variable contains download file url path.

Using Invoke-RestMethod, it download file from specified url and save it to location.

PowerShell Download Zip File from Url

The following steps describes PowerShell script to download zip file from internet and unzip it

Assign Download zip file from url path to 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"

Above command will assign github repository mastter.zip url path to $Url variable

Get file name along with extension from url using Split-Path cmdlet

$DownloadZipFile = "D:\PowerShell\Zip\ZipFile\" + $(Split-Path -Path $Url -Leaf)

Above command will return filename along with extension eg.. master.zip and assign it to $DownloadZipFile variable.

PowerShell Tip: Download 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 above url to save response body and create shell object

Invoke-WebRequest -Uri $Url -OutFile $DownloadZipFile
$ExtractShell = New-Object -ComObject Shell.Application 

PowerShell extract zip file items

$ExtractFiles = $ExtractShell.Namespace($DownloadZipFile).Items()

Use ExtractFiles to copy extracted file to destination folder and start a process

$ExtractShell.NameSpace($ExtractPath).CopyHere($ExtractFiles) 
Start-Process $ExtractPath

Above guide will help you to understand step by step procedure about using PowerShell script to download file from url and unzip file to destination folder.

Copy below Powershell download file from url script in ISE and run it. Make sure to give DownloadZipFile path and ExtractFilePath correct, otherwise it may throws error if folder or drive 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 to unzip a file to folder using PowerShell.

PowerShell Download File using Invoke-RestMethod

In PowerShell, download file from url using Invoke-RestMethod cmdlet is similar to Invoke-WebRequest and uses the same parameters like source url and destination to copy zip file on drive.

Lets consider an example, to download 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 to download file from 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, download file from url,

  • $source variable has github main.zip file url assigned.
  • $destination variable contains folder path to copyhere zip file.
  • Using PowerShell Invoke-RestMethod cmdlet, use $source and $destination parameters to download file from url in PowerShell.

Cool Tip: How to use Stop-Process to kill process in PowerShell!

Conclusion

We have covered steps to download file from url, download zip file and extract zip file to destination folder.

Using PowerShell Invoke-WebRequest to download file from url and extract zip file to folder on destination location.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.

Leave a Comment