How to download images using Invoke-Webrequest in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To download the images from the webpage using the Invoke-WebRequest command, we can use the images property from the result to retrieve the images URL, and later we can use the method to download them at the specific location. Consider we have the URI: https://theautomationcode.com to retrieve the images.

Once you run the below command, you can see the Images property there.

Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"

To retrieves the images URL,

$req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"
$req.Images | Select -ExpandProperty src

Output

https://i1.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-9.png?resize=178%2C60&ssl=1 https://i0.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-10.png?resize=640%2C68&ssl=1

All the above URLs point to the images, so we can download that.

$wc = New-Object System.Net.WebClient
$req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"
$images = $req.Images | Select -ExpandProperty src
$count = 0
foreach($img in $images){    
   $wc.DownloadFile($img,"C:\Temp\WebImages\img$count.jpg")
}

The output images will be stored in the C:\temp\WebImages folder.

raja
Published on 18-Jan-2021 07:25:15
Advertisements