Automating Downloads from SourceForge

Adam Bertram

Adam Bertram

Read more posts by this author.

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.

PS> $uri = 'http://downloads.sourceforge.net/project/itextsharp/itextsharp/iTextSharp-5.5.9/itextsharp-all-5.5.9.zip?r=&ts=1460913874&use_mirror=jaist'
PS> 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.

PS> $uri = 'http://downloads.sourceforge.net/project/itextsharp/itextsharp/iTextSharp-5.5.9/itextsharp-all-5.5.9.zip?r=&ts=1460913874&use_mirror=jaist'
PS> 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.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!