Paul
Paul

Reputation: 43

Downloading files with powershell 2.0 on windows 7

Here's my script to download the file, im replacing the url to download from because it's a private cloud site, i've searched everywhere and they all say that this is the right way to download in older versions of PS but i keep getting the error

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." At line:60 char:25 + $client.DownloadFile <<<< ($url, $targetFile) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

$url = "(download url)" 
$path = "C:\temp\jre-7u71-windows-i586.exe" 
# param([string]$url, [string]$path) 

if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
  $targetFile = Join-Path $pwd (Split-Path -leaf $path) 
} 

"Downloading [$url]`nSaving at [$path]" 
$client = new-object System.Net.WebClient 
$client.DownloadFile($url, $targetFile) 
#$client.DownloadData($url, $targetFile) 

$path

Upvotes: 4

Views: 16340

Answers (1)

Stiegler
Stiegler

Reputation: 739

I had the same problem and there seems to be a problem in the way you declared your $client variable.

Old post, but this works for me in Windows 7, PowerShell 2.0

Please note that URL and Path are changed.

$url = "http://www.7-zip.org/a/7z1604.exe" 
$path = "C:\temp\7z1604.exe" 
# param([string]$url, [string]$path) 

if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
$targetFile = Join-Path $pwd (Split-Path -leaf $path) 
} 

(New-Object Net.WebClient).DownloadFile($url, $path) 
$path

Upvotes: 6

Related Questions