Home > Software engineering >  Download File on Webpage via Windows CMD/Power Shell
Download File on Webpage via Windows CMD/Power Shell

Time:02-10

just as the title states, I'd like to download a file from the internet, specifically the download on this webpage. I have looked into using Invoke-WebRequest, curl, and certutil. All of these options download the HTML of the site. The specific URL of the download looks like this: https://bluetoothinstaller.com/bluetooth-command-line-tools/BluetoothCLTools-1.2.0.56.exe.

Calling things like the following just downloads the HTML:

Invoke-WebRequest -Uri 'https://bluetoothinstaller.com/bluetooth-command-line-tools/BluetoothCLTools-1.2.0.56.exe' -OutFile 'test.exe'

Alternatively, if anyone knows how to download the link via the HTML, please do share.

I'd prefer it if the solution did not require any additional software, but am flexible.

Thanks!

CodePudding user response:

Looking at some code I wrote near the end of last year and I found this line:

(New-Object System.Net.WebClient).DownloadFile($URL, $ZipFile)

In my case I was trying to download the latest SQLite and it worked. In your case you will probably want to rename the $ZipFile variable to something like $ExeFile.

The command to build the file path/name, and define where I wanted the file saved, was this:

$ZipFile = "$PSScriptRoot\$(Split-Path -Path $URL -Leaf)" 

As for extracting the file's download path form a webpage, I haven't done that yet. It is something aim to do but it will be awhile before I get around to trying to figure that out.

CodePudding user response:

The following worked for me, note the OutFile comment. You might find something useful on the Network tab of your browser's dev-tools.

$params = @{
    UseBasicParsing = $true
    Uri = "https://bluetoothinstaller.com/bluetooth-command-line-tools/BluetoothCLTools-1.2.0.56.exe"
    Headers = @{
        Referer = "https://bluetoothinstaller.com/bluetooth-command-line-tools/download.html"
    }
    OutFile = 'path/to/download.exe' # Change this
}
Invoke-RestMethod @params
  • Related