0

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

haku
  • 4,105
  • 7
  • 38
  • 63

3 Answers3

1

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

Community
  • 1
  • 1
haku
  • 4,105
  • 7
  • 38
  • 63
0

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()
Brian H
  • 9
  • 3
  • Though `ProcessStartInfo` offers a lot more customization, this particular scenario could also be written as `$ProcessExitCode = (Start-Process -FilePath $Path -ArgumentList $Args -PassThru -Wait).ExitCode`. The key point for this question, of course, is the string interpolation with `"$($env:userprofile)\es-cli\es.exe"`. – Lance U. Matthews Dec 24 '19 at 23:35
0

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
js2010
  • 23,033
  • 6
  • 64
  • 66