Search code examples
windowspowershelldownloadcredentialsinvoke-webrequest

Download Files with Password in Powershell


I need to download a 51gb zip file from the internet, to open the website you need to enter a password.

I want to do this with a function in PowerShell(PSVersion:5.1.18362.752), but I can't get any further. My function looks like this:

 $securepassword = "thepassword"

function Get-Files {
   
    $Properties = @{
        URI        = "https://theurl"
        Credential = $securepassword
    }
    Invoke-WebRequest @Properties -OutFile "C:\Users\$env:USERNAME\Desktop\thefiles.zip"

}

Can the parameter Credential only be used for Windows Credential?

Many thanks for the help


Solution

  • I think you are using Credentials wrong. Credentials is not password only. It is username plus password, and they should be instance of ICredentials

    Please note, that credentials can be passed this way ONLY for web pages that request HTTP 401 authentication. If web page uses form-based authentication, you should process it as non-standard case and credentials will not work, you will need to post data, keep cookies, etc.

    $localPath = [System.IO.Path]::Combine(
        [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::DesktopDirectory),
        'OutFileName.zip')
    
    $wc = [System.Net.WebClient]::new()
    $wc.Credentials = [System.Net.NetworkCredential]::new($username, $plainTextPassword)
    $wc.DownloadFile($uri, $localPath)
    $wc.Dispose()