1

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"
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • Please include any error messages that you receive, and any other information regarding what you have tried in order to get it working. – Jeff Zeitlin Jan 27 '17 at 12:55

2 Answers2

1

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%.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
0

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.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40