,

How to download files with Powershell

1–2 minutes
10 comments on How to download files with Powershell

Powershell HeaderThis should show you how you can download a file with Powershell. This is not a script or function you should use. It just is the the easyiest way to download a file with Powershell. If I have enough time I will create a function for downloading files.

$Url = "https://www.thomasmaurer.ch/ps.txt"
$Path = "C:\temp\ps.txt"
$Username = ""
$Password = ""

$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $url, $path )

10 responses

  1. Anony Avatar

    It didn’t run for me.

    Here is what I got:

    \PS C:\Users\emiller> $Url = “https://www.thomasmaurer.ch/ps.txt”
    $Path = “C:\temp\ps.txt”
    $Username = “”
    $Password = “”

    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
    $WebClient.DownloadFile( $url, $path )
    Exception calling “DownloadFile” with “2” argument(s): “The remote server returned an error: (407) Proxy Authentication Required
    .”
    At line:8 char:24
    + $WebClient.DownloadFile <<<< ( $url, $path )
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

  2. Suresh Avatar

    Hai,

    I am getting below error, Can you please help me?

    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
    $WebClient.DownloadFile( “$url”, “$path” )
    Exception calling “DownloadFile” with “2” argument(s): “An exception occurred during a WebClient request.”
    At line:9 char:1
    + $WebClient.DownloadFile( “$url”, “$path” )
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

  3. Jack Avatar

    You’re getting errors because you initialized the variables as $Url and $Path but then below you wrote $url and $path (notice the missing capitol letters)
    should work once you fix that.

    1. Thomas Maurer Avatar

      Thanks for your anwser I will check that

  4. kaustubh Avatar
    kaustubh

    Command is working fine with windows server 2012 (powershell 4.0) is not working with windows 8 (powershell 4.0) for same.

    I want to download file from iis-server.

    (New-Object System.Net.WebClient).DownloadFile(‘http://server12/vdir/v.exe’,’C:\pqr.exe’)
    I tried this. It worked fine for me in server 2012, but in windows 8 it gave me method invocation error

    Exception calling “DownloadFile” with “2” argument(s): “An exception occurred during a WebClient request.

  5. Hector Avatar

    Script worked great.

    Thanks Thomas

  6. Dadaso Pawar Avatar
    Dadaso Pawar

    New-Object : Cannot find type [System.Net.WebClient]: verify that the assembly containing this type is loaded.
    At /home/dadaso/Desktop/fileupload.ps1:13 char:14
    + $webclient = New-Object System.Net.WebClient
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

    The property ‘Credentials’ cannot be found on this object. Verify that the property exists and can be set.
    At /home/dadaso/Desktop/fileupload.ps1:14 char:1
    + $webclient.Credentials = New-Object System.Net.NetworkCredential($use …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

    You cannot call a method on a null-valued expression.
    At /home/dadaso/Desktop/fileupload.ps1:15 char:1
    + $WebClient.DownloadFile( $url, $path )
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

  7. Mark Avatar

    This like many other examples will work fine but what about multiple files where
    a) you do not have to name these files
    b) point to a url and pull from there?

  8. meg Avatar

    How do I use the Windows Logon Credentials or Single Sign on of gmail ?
    Please help

    Thanks in advance
    Currently, I am prompting for the credentials. But we want to use the Single-Signon or the Windows Logon credentials
    Below is my code:
    #define the URL and credentials#
    $Url = “MY_URL”
    $Path_URL = “MY_PATH”
    $Username = Read-Host “Enter Your Email Address for Cylance Login:”
    $Password = Read-Host “Enter your Password:” -AsSecureString -ConvertTo-SecureString
    Write-Host “`n `n Succesfully Logged in `n `n”
    #Download#

    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
    $WebClient.DownloadFile( $url, $Path_URL )

  9. preetham Avatar
    preetham

    Exception calling “DownloadFile” with “2” argument(s): “An exception occurred during a WebClient request.”
    At line:7 char:5
    + $WebClient.DownloadFile($Url, $Path)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
    Getting this error
    using windows 10 and windows poweshell ISE

Leave a Reply

Your email address will not be published. Required fields are marked *

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