tehseen sagar
tehseen sagar

Reputation: 11

Powershell file download issue

I'm trying to download the PuTTY executable using PowerShell, but could not get the file on temp path.

My script:

$Url = "https://the.earth.li/~sgtatham/putty/latest/x86/putty.exe"
$Path = "C:%homepath%\AppData\Local\Temp\putty.exe"
$Wc = New-Object System.Net.WebClient
$Wc.DownloadFileAsync($Url,$Path)

I am executing following command via CMD:

powershell.exe "-ExecutionPolicy" "RemoteSigned" "-file" "test.ps1"

Upvotes: 1

Views: 365

Answers (2)

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10764

You have two problems, both of which need to be corrected for your script to have a chance of working.

  1. The command for executing a Powershell script from within CMD.EXE should not have the arguments quoted:

    powershell.exe -ExecutionPolicy RemoteSigned -file test.ps1

  2. To expand a system environment variable from within powershell, you do not surround it with % as you do in CMD. See http://ss64.com/ps/syntax-env.html for more information; assuming that the environment variable HOMEPATH exists, you would reference it in Powershell as $env:homepath, not %homepath%.

Upvotes: 1

henrycarteruk
henrycarteruk

Reputation: 13227

The %VAR% form is not used in powershell, this is only used in CMD. In PowerShell you need to use $env:VAR instead.

You can run Get-ChildItem Env: to get a list of all the Environmental Variables you can use.

For your script try this:

$Path = "$env:USERPROFILE\AppData\Local\Temp\putty.exe"

I've used USERPROFILE instead of HOMEPATH as this includes the drive letter so your script will still work if a different letter is used.

Upvotes: 0

Related Questions