How Do You Get PowerShell’s Invoke-Webrequest to Work Consistently (Instead of “command not found”)?

Problem scenario
You use invoke-webrequest and it works fine with some websites. But sometimes you find that PowerShell says "command not found."  What is wrong?

Solution
invoke-webrequest will work to download html and htm files.  Depending on the source file on the web server, the command needs a flag to work properly.  For an image file with a .jpeg extension, the invoke-webrequest needs a flag "-ContentType 'image/jpg'" or something similar to work.  The flag depends on the file that you are downloading from a web server.  This command below is an example of a non-HTML file that you want to download with PowerShell (the file happens to be an image file named foobar.jpeg).

invoke-webrequest http://source/URL/foobar.jpeg -ContentType 'image/jpg'

If you do not need to do type checking when it downloads, try a wildcard:

invoke-webrequest http://source/URL/foobar.jpeg -ContentType *

Leave a comment

Your email address will not be published.