Download Images Using Invoke-WebRequest in PowerShell



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.

Updated on: 2021-01-18T07:26:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements