This 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.
Thomas is the EMEA Global Black Belt (GBB) for Sovereign Cloud at Microsoft, helping organizations across the region harness Microsoft’s sovereign, hybrid, multicloud, and edge solutions. He works directly with C-level leaders and technical teams to remove roadblocks, accelerate digital transformation, and shape long-term cloud strategies that meet business and regulatory needs.
With over 15 years of experience in cloud and datacenter, including his previous role as Principal Program Manager and Chief Evangelist for Azure Hybrid, he combines deep technical expertise with a passion for customer success. He specializes in creating scalable architectures, solution accelerators, and thought leadership that drive adoption and innovation on Azure.
Thomas is a passionate community advocate and frequently speaks at industry events such as Microsoft Ignite and Microsoft TechDays. He shares his expertise through his Cloud and Datacenter Blog at www.thomasmaurer.ch and on LinkedIn and X.com www.twitter.com/thomasmaurer.
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.
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.
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
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?
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#
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
My name is Thomas Maurer. I am a EMEA Global Black Belt (GBB) for Sovereign Cloud at Microsoft. I am part of Azure Cloud & AI Platforms and engage with the community and customers around the world. Opinions are my own.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
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
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
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.
Thanks for your anwser I will check that
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.
Script worked great.
Thanks Thomas
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
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?
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 )
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