haku
haku

Reputation: 4505

In powershell, how do I specify path to an exe including $env:userprofile?

I am trying to create a script to run an exe that would be located inside userprofile folder, but I cant seem to get the command working. Any idea how I can get this working?

I tried: $env:userprofile\es-cli\es.exe myParameter

I got an error saying: Unexpected token '\es-cli\es.exe' in expression or statement.

Also tried:

($env:userprofile)\es-cli\es.exe myParameter got an error unexpected token \es-cli\es.exe

`$($env:userprofile)\es-cli\es.exe myParameter` got an error the term $ is not recognized as the name of a cmdlet...

$loc = "{0}\es-cli\es.exe" -f $env:userprofile $loc myParameter # cant do this because $loc is a string

Upvotes: 1

Views: 1864

Answers (3)

js2010
js2010

Reputation: 27423

Using the call operator should work:

& $env:userprofile\es-cli\es.exe myParameter

Assuming you don't want to just add it to the path:

$env:path += ";$env:userprofile\es-cli"
es myParameter

Upvotes: 0

Brian H
Brian H

Reputation: 9

I know this is a lot more code. But I like it as it give you more control over how your launched program behaves, stats, getting return codes, and errors back when needed. This is the long way to do the Startprocess method.

$Path = "$($env:userprofile)\es-cli\es.exe"
$Args = "myParameter"
$newProcess = new-object System.Diagnostics.ProcessStartInfo $Path;
$newProcess.Arguments = $Args
$RunPro2 = New-Object System.Diagnostics.Process
$RunPro2.StartInfo = $NewProcess
$RunPro2.Start()
$RunPro2.WaitForExit()
$ProcessExitCode = $RunPro2.ExitCode.ToString()

Upvotes: 0

haku
haku

Reputation: 4505

Found a solution. Thanks to this SO post. I ended up doing below:

& ("{0}\es-cli\es.exe" -f $env:userprofile) myParameter

Based on the comment from Mathias below, I ended up using:

&(Join-Path $env:USERPROFILE 'es-cli\es.exe') myParam

Upvotes: 2

Related Questions