The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

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 with webclient :
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:

–jeroen



@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

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.