Tormod Haugene Tormod Haugene - 1 month ago 5
Bash Question

Using variable in execution path

I am trying to store a folder path as a variable in PowerShell, so that I can reuse that path for various task throughout my script:

# Local storage location
$env:localTempFolder="TEMP"

# Download some executable and store it in the local temp folder
$env:downloadURL="http://somepage.com/foo.exe"
Invoke-WebRequest $env:downloadURL -OutFile $env:localTempFolder/foo.exe

# Execute the executable using the $localTempFolder variable
./$env:localTempFolder/foo.exe


The last statement gives the following error:

.\$env:localTempFolder\.exe : The term '.\$env:pci_TEMP\foo.exe' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.

At line:1 char:1
+ .\$env:pci_TEMP\foo.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\$env:localTempFolder\foo.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


I have tried adding various combinations of parentheses, quotes etc. to the last statement, but does not seem to get it right. What am I doing wrong, and where do I go from here?

Answer

As PetSerAl pointed it out, you are missing the call operator &. I would also suggest you to use the Join-Path cmdlet to combine a path:

& (Join-Path $env:localTempFolder 'foo.exe')
Comments