Automating Downloads from SourceForge

Adam Bertram

Adam Bertram

Read more posts by this author.

Occasionally I need to download some utilities from 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 = ''
PS> Invoke-WebRequest -Uri $uri -OutFile

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 = ''
PS> Invoke-WebRequest -Uri $uri -OutFile -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!