# WebClient DownloadFile problem

**URL:** <https://forums.powershell.org/t/webclient-downloadfile-problem/4619>\
**Category:** PowerShell Help\
**Created:** [August 4, 2015, 4:00am UTC](https://forums.powershell.org/t/webclient-downloadfile-problem/4619 "2015-08-04T04:00:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-mantha](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@joe-mantha](https://forums.powershell.org/u/joe-mantha)\
**Post date:** [August 4, 2015, 4:00am UTC](https://forums.powershell.org/t/webclient-downloadfile-problem/4619/1 "2015-08-04T04:00:45Z")

</div>

Hello,  
I’m new to powershell but I think I’m trying to do something pretty simple. I probably just have a syntax error!

I’ve written this command:

```
(New-Object System.Net.WebClient).DownloadFile("http://MySite/fileextract.asp?userid=MyUser`&password=MyPassword`&file=MyFile.zip","c:\Download\MyFile.zip")
```

Which runs successfully in Powershell.

The problem I’m having is that when I try to use to Powershell.exe -command syntax in a batch file, I keep getting an error:

```
PowerShell.exe -Command {(New-Object System.Net.WebClient).DownloadFile("http://MySite/fileextract.asp?userid=MyUser`&password=MyPassword`&file=MyFile.zip","c:\Download\MyFile.zip")}
```

I get an unexpected token error on the coma character. Notice that there is two & characters in the url that I had to escape with `.

What I’m I doing wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 4, 2015, 4:18am UTC](https://forums.powershell.org/t/webclient-downloadfile-problem/4619/2 "2015-08-04T04:18:33Z")

</div>

powershell.exe has the -EncodedCommand parameter specifically to help you deal with weird parsing issues like this. You just need to convert your command to a base64 string (examples for this are in powershell.exe /?). For example:

```
$code = @'
(New-Object System.Net.WebClient).DownloadFile("http://MySite/fileextract.asp?userid=MyUser`&password=MyPassword`&file=MyFile.zip","c:\Download\MyFile.zip")
'@

$unicodeBytes = [System.Text.Encoding]::Unicode.GetBytes($code)
$base64 = [System.Convert]::ToBase64String($unicodeBytes)
Set-Clipboard $base64
```

If you run that code, it puts the base64 representation of your command in the clipboard (assuming you’re running a version of PowerShell with the Set-Clipboard command, of course.) Then you just need to call powershell.exe like this:

```
powershell.exe -EncodedCommand KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACIAaAB0AHQAcAA6AC8ALwBNAHkAUwBpAHQAZQAvAGYAaQBsAGUAZQB4AHQAcgBhAGMAdAAuAGEAcwBwAD8AdQBzAGUAcgBpAGQAPQBNAHkAVQBzAGUAcgBgACYAcABhAHMAcwB3AG8AcgBkAD0ATQB5AFAAYQBzAHMAdwBvAHIAZABgACYAZgBpAGwAZQA9AE0AeQBGAGkAbABlAC4AegBpAHAAIgAsACIAYwA6AFwARABvAHcAbgBsAG8AYQBkAFwATQB5AEYAaQBsAGUALgB6AGkAcAAiACkA
```

---

<div class="post-metadata">

**Author:** ![joe-mantha](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@joe-mantha](https://forums.powershell.org/u/joe-mantha)\
**Post date:** [August 4, 2015, 4:26am UTC](https://forums.powershell.org/t/webclient-downloadfile-problem/4619/3 "2015-08-04T04:26:52Z")

</div>

Great, this works perfectly!

Thanks alot!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/webclient-downloadfile-problem/4619/4 "2024-05-16T20:44:59Z")

</div>

Closed on May 16, 2024, 8:44pm UTC


