Quick batch file hack to download a file calling PowerShell to do the heavy lifting
Posted by jpluimers on 2025/08/12
I needed this download-file.bat a while ago, but forgot how I found out.
It’s in this gist too: [Wayback/Archive] Quick batch file hack to download a file calling PowerShell to do the heavy lifting.
Here we go:
@echo off if "%1"=="" goto :help @echo on powershell.exe -Command (New-Object System.Net.WebClient).DownloadFile('%1','%2') @echo off goto :eof :help echo Syntax: %0 URL local-filename
The echo off/echo on are to make it easier to see what PowerShell is doing.
You can add more to this, for instance user agent as explained in [Wayback/Archive] http – How to download a file with Powershell System.Net.WebClient and custom user-agent string? – Stack Overflow (thanks [Wayback/Archive] slantalpha and [Wayback/Archive] ClumsyPuffin)
Q
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://domain.name/file.name','C:\file.name')"Is there a way to customize the user-agent and to also retain the one-line command format?A
This code snippet will perform the custom user agent part along withwebclient:powershell -command { $cli = New-Object System.Net.WebClient; $cli.Headers['User-Agent'] = 'myUserAgentString'; $cli.DownloadFile('https://domain.name/file.name', 'C:\file.name') }C
Thanks, this worked:
powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://domain.name/file.name', 'C:\file.name')"
Links:
- [Wayback/Archive]
New-Object(Microsoft.PowerShell.Utility) – PowerShell | Microsoft Learn - [Wayback/Archive]
WebClientClass (System.Net) | Microsoft Learn - [Wayback/Archive] WebClient.
DownloadFileMethod (System.Net) | Microsoft Learn - [Wayback/Archive] WebClient.
HeadersProperty (System.Net) | Microsoft Learn - [Wayback/Archive]
User-Agent– HTTP | MDN - [Wayback/Archive] System.Net.WebClient – Google Search
–jeroen
https://gist.github.com/jpluimers/866c1402e92f41dd8da8cfd45763b785






Leave a Comment