Occasionally I need to download some utilities from SourceForge.net via PowerShell. At first, I attempted to do what I usually do with others sites. I'll find the direct link to the file I'm looking for, copy it out and paste it into the Uri parameter to Invoke-WebRequest.

$uri = 'http://downloads.sourceforge.net/project/itextsharp/itextsharp/iTextSharp-5.5.9/itextsharp-all-5.5.9.zip?r=&ts=1460913874&use_mirror=jaist'
Invoke-WebRequest -Uri $uri -OutFile iTextSharp.zip

When doing this I noticed that the URI wasn't a direct link to the file I needed. Even though the link wasn't a direct link to the file I thought that wouldn't be a problem since Invoke-WebRequest follows any redirects. But, it didn't work this time.

After a bit of Googling, I found the default user agent being used by Invoke-WebRequest �would not work. Instead, I needed to manually specify a "normal" user agent from a typical web browser like IE, Firefox or Chrome so I decided to use Chrome.

$uri = 'http://downloads.sourceforge.net/project/itextsharp/itextsharp/iTextSharp-5.5.9/itextsharp-all-5.5.9.zip?r=&ts=1460913874&use_mirror=jaist'
Invoke-WebRequest -Uri $uri -OutFile iTextSharp.zip -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome

After I specified the user agent, the download worked as expected! Keep this trick in mind next time you need to download something in a script from SourceForge.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!