Discussion:
running a program with spaces in Path with invoke-expression
(too old to reply)
Frank
2008-04-24 03:51:01 UTC
Permalink
Hi,

I have a situation where I need to define the executable with the path to be
used with invoke-expression. It works with putting the path in there but
because the path has a space, an error becomes of it. The program path has a
space in it where it is:

$testcmd = "C:\Program Files\test\testutil"

$CMD = "$testcmd param1"
invoke-expression $CMD

When I execute this, I get:

The term 'C:\Program' is not recognized as a cmdlet, function, operable
program, or script file. Verify the term and try again.

When I use & in place of invoke-expression, I also get an error. Is there a
way around this other than putting the directory of the executable in the
path?

Thanks in advance,
Spider Pig
2008-04-24 13:02:40 UTC
Permalink
This is not a solution to your problem but might help towards a solution...

I did the following after reading your question:

$testcmd = "C:\Program Files\Movie Maker\moviemk.exe"

invoke-expression $testcmd - this gives an error
& $testcmd - this works

Well, to me this indicates that there is a difference between using "&" and
"invoke-expression"

Hmm, seems that "&" = "invoke-item" NOT "invoke-expression"

invoke-item $testcmd - works

Lets try with the parameter:

& $testcmd param1 - works too

BUT

invoke-item $testcomd param1 - no go

so "&" is not equal to "invoke-item" either.

As soon as you try and combine the app. path and the param it doesn't work
either , e.g.
$cmd = "$testcmd param1"
& $cmd
Marc Magnin
2009-07-29 10:18:27 UTC
Permalink
Hello

I'm replying to this old post because it helped me to find a way t
dynamically call / run program requiring arguments like

C:\Program files\Program\program.exe /verysilent /norestar
/LoadInf=".\conf.ini

This work if I write directly the full command in powershell like

Code
-------------------

& C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini

-------------------

Great
But I have no idea of the command in advance... Heck
So I tried several things like

Code
-------------------

& $fullCommandStrin
& $commandString $argsStrin
. $commandString $argsStrin
. $quotedCommandString $quotedArgsStrin
powershell -command "& {" $quotedCommandString $quotedArgsString "}
invoke-item $commandString $argsStrin

-------------------

etc.......

but I still had errors caused by space in "Program files" or slash o
arguments etc..

The way I found is to pass a string for the command and an array o
string for arguments
(here I directly wrote the command in the code by in reality, $comman
is feeded from and XML file

Code
-------------------

$command = "C:\Program files\Program\program.exe /verysilent /norestart /LoadInf='.\conf.ini'
if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)")
$args = [regex]::Split($Matches.appArgs.trim(), "\s"
& $Matches.appPath.trim() $arg


-------------------

Hope thats helps

Marc Magnin
Pourquoi faire simple quand on peut faire compliqué ?

--
Marc Magnin
Marc Magnin
2009-07-29 11:22:00 UTC
Permalink
I'have just found a better way by using the [diagnostics.process] clas
cause i need to wait the end of execution :

Code
-------------------

if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)")
$com =$Matches.appPath.trim(
$args = $Matches.appArgs.trim(
[diagnostics.process]::start($com, $args).WaitForExit(


-------------------


Marc Magnin
Pourquoi faire simple quand on peut faire compliqué ?

--
Marc Magnin
stej
2009-07-29 12:17:03 UTC
Permalink
I replied only to author by mistake, so once again:
Start-process works ok for me. Just try:

Start-Process -FilePath "c:\Program Files\Internet Explorer
\iexplore.exe" -ArgumentList 'http://www.seznam.cz'
Marc Magnin
2009-07-29 13:35:57 UTC
Permalink
Hi

Start-Process is only available in PowerShell Community Extensions
unfortunately, these extensions are not available in our office..

A good link about processes and powershell
'Windows PowerShell Blog : Managing Processes in PowerShell
(http://blogs.msdn.com/powershell/archive/2007/01/16/managing-processes-in-powershell.aspx

-mar
Pourquoi faire simple quand on peut faire compliqué ?

--
Marc Magnin
tojo2000
2009-07-29 21:04:05 UTC
Permalink
Hi,
Start-Process is only available in PowerShell Community Extensions,
unfortunately, these extensions are not available in our office...
'Windows PowerShell Blog : Managing Processes in PowerShell'
(http://blogs.msdn.com/powershell/archive/2007/01/16/managing-processe...)
-marc
Pourquoi faire simple quand on peut faire compliqué ?!
--
Marc Magnin
If you don't have Start-Process you could still try using

[System.Diagnostics.Process]::Start($your_command)

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
OldDog
2009-07-30 02:47:56 UTC
Permalink
Post by tojo2000
Hi,
Start-Process is only available in PowerShell Community Extensions,
unfortunately, these extensions are not available in our office...
'Windows PowerShell Blog : Managing Processes in PowerShell'
(http://blogs.msdn.com/powershell/archive/2007/01/16/managing-processe...)
-marc
Pourquoi faire simple quand on peut faire compliqué ?!
--
Marc Magnin
If you don't have Start-Process you could still try using
[System.Diagnostics.Process]::Start($your_command)
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.st...
Not to be old fashioned, but I use the Short Path name in these cases.
You can find them by doing this:

C:\>dir *. /x
Volume in drive C is OS
Volume Serial Number is 66AB-C4DB

Directory of C:\

04/18/2007 04:37 PM <DIR> ATI
04/07/2007 12:53 PM <DIR> DELL
04/02/2007 01:36 PM <DIR> doctemp
04/02/2007 01:36 PM <DIR> Drivers
06/20/2009 11:54 AM <DIR> ISO
06/16/2007 11:46 PM <DIR> PHOTOS~1 Photoshop 7
06/21/2009 01:29 PM <DIR> PROGRA~1 Program Files
<-----
07/17/2009 09:01 AM <DIR> PSTools
07/12/2009 08:43 PM <DIR> Scripts
09/15/2007 07:07 PM <DIR> STARTM~1 Start Menu
06/28/2009 05:28 PM <DIR> Temp
04/07/2007 12:25 PM <DIR> Users
07/01/2009 05:41 PM <DIR> Windows

Then this: dir "Program Files" /x

Which shows you this:

07/29/2009 03:07 AM <DIR> INTERN~1 Internet Explorer
<----

So your command line looks like this:

"c:\Progra~1\INTERN~1\iexplore.exe"

Works for me.

OldDog
Larry__Weiss
2009-07-30 03:41:57 UTC
Permalink
You have to be a little careful about expectations of the short names.
They aren't always the same on all systems. The "-1" "-2" etc, get assigned
first-created first-assigned, in my experience for names that are the same in
the first 6 characters.

- Larry
Post by OldDog
Not to be old fashioned, but I use the Short Path name in these cases.
C:\>dir *. /x
Volume in drive C is OS
Volume Serial Number is 66AB-C4DB
Directory of C:\
04/18/2007 04:37 PM <DIR> ATI
04/07/2007 12:53 PM <DIR> DELL
04/02/2007 01:36 PM <DIR> doctemp
04/02/2007 01:36 PM <DIR> Drivers
06/20/2009 11:54 AM <DIR> ISO
06/16/2007 11:46 PM <DIR> PHOTOS~1 Photoshop 7
06/21/2009 01:29 PM <DIR> PROGRA~1 Program Files
<-----
07/17/2009 09:01 AM <DIR> PSTools
07/12/2009 08:43 PM <DIR> Scripts
09/15/2007 07:07 PM <DIR> STARTM~1 Start Menu
06/28/2009 05:28 PM <DIR> Temp
04/07/2007 12:25 PM <DIR> Users
07/01/2009 05:41 PM <DIR> Windows
Then this: dir "Program Files" /x
07/29/2009 03:07 AM <DIR> INTERN~1 Internet Explorer
<----
"c:\Progra~1\INTERN~1\iexplore.exe"
Works for me.
OldDog
OldDog
2009-07-30 13:03:50 UTC
Permalink
Post by Larry__Weiss
You have to be a little careful about expectations of the short names.
They aren't always the same on all systems.  The "-1" "-2" etc, get assigned
first-created first-assigned, in my experience for names that are the same in
the first 6 characters.
  - Larry
Post by OldDog
Not to be old fashioned, but I use the Short Path name in these cases.
C:\>dir *. /x
 Volume in drive C is OS
 Volume Serial Number is 66AB-C4DB
 Directory of C:\
04/18/2007  04:37 PM    <DIR>                       ATI
04/07/2007  12:53 PM    <DIR>                       DELL
04/02/2007  01:36 PM    <DIR>                       doctemp
04/02/2007  01:36 PM    <DIR>                       Drivers
06/20/2009  11:54 AM    <DIR>                       ISO
06/16/2007  11:46 PM    <DIR>          PHOTOS~1     Photoshop 7
06/21/2009  01:29 PM    <DIR>          PROGRA~1     Program Files
<-----
07/17/2009  09:01 AM    <DIR>                       PSTools
07/12/2009  08:43 PM    <DIR>                       Scripts
09/15/2007  07:07 PM    <DIR>          STARTM~1     Start Menu
06/28/2009  05:28 PM    <DIR>                       Temp
04/07/2007  12:25 PM    <DIR>                       Users
07/01/2009  05:41 PM    <DIR>                       Windows
Then this:  dir "Program Files" /x
07/29/2009  03:07 AM    <DIR>          INTERN~1     Internet Explorer
<----
"c:\Progra~1\INTERN~1\iexplore.exe"
Works for me.
OldDog
You can try this:

$a = New-Object -ComObject Scripting.FileSystemObject
$f = $a.GetFile("C:\Program Files\Internet Explorer\ExtExport.exe")
$f.ShortPath
John Hart
2022-06-10 16:30:26 UTC
Permalink
Post by OldDog
Post by tojo2000
Hi,
Start-Process is only available in PowerShell Community Extensions,
unfortunately, these extensions are not available in our office...
'Windows PowerShell Blog : Managing Processes in PowerShell'
(http://blogs.msdn.com/powershell/archive/2007/01/16/managing-processe...)
-marc
Pourquoi faire simple quand on peut faire compliqué ?!
--
Marc Magnin
If you don't have Start-Process you could still try using
[System.Diagnostics.Process]::Start($your_command)
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.st...
Not to be old fashioned, but I use the Short Path name in these cases.
C:\>dir *. /x
Volume in drive C is OS
Volume Serial Number is 66AB-C4DB
Directory of C:\
04/18/2007 04:37 PM <DIR> ATI
04/07/2007 12:53 PM <DIR> DELL
04/02/2007 01:36 PM <DIR> doctemp
04/02/2007 01:36 PM <DIR> Drivers
06/20/2009 11:54 AM <DIR> ISO
06/16/2007 11:46 PM <DIR> PHOTOS~1 Photoshop 7
06/21/2009 01:29 PM <DIR> PROGRA~1 Program Files
<-----
07/17/2009 09:01 AM <DIR> PSTools
07/12/2009 08:43 PM <DIR> Scripts
09/15/2007 07:07 PM <DIR> STARTM~1 Start Menu
06/28/2009 05:28 PM <DIR> Temp
04/07/2007 12:25 PM <DIR> Users
07/01/2009 05:41 PM <DIR> Windows
Then this: dir "Program Files" /x
07/29/2009 03:07 AM <DIR> INTERN~1 Internet Explorer
<----
"c:\Progra~1\INTERN~1\iexplore.exe"
Works for me.
OldDog
Soooo this little trick worked like a charm for me just now! I appreciate you putting it out there, good ole 'OLD SCHOOL'!
Thanks OldDog!

Marc Magnin
2009-07-31 14:46:08 UTC
Permalink
Hello

Actually i work with this

Code
-------------------

$command = "C:\Program files\foo\foo.exe
$arguments = "/verysilent /norestart /LoadInf='.\foo.ini'
$startinfo = new-object System.Diagnostics.ProcessStartInf
$startinfo.FileName = $command.fileNam
$startinfo.Arguments = $command.argument
[System.Diagnostics.Process]::Start($startinfo

-------------------

This is quite perfect for me ;

Best regards

-mar
Pourquoi faire simple quand on peut faire compliqué ?

--
Marc Magnin
RickB
2008-04-24 14:02:55 UTC
Permalink
Post by Frank
Hi,
I have a situation where I need to define the executable with the path to be
used with invoke-expression.  It works with putting the path in there but
because the path has a space, an error becomes of it.  The program path has a
$testcmd = "C:\Program Files\test\testutil"
$CMD = "$testcmd param1"
invoke-expression $CMD
The term 'C:\Program' is not recognized as a cmdlet, function, operable
program, or script file. Verify the term and try again.
When I use & in place of invoke-expression, I also get an error. Is there a
way around this other than putting the directory of the executable in the
path?
Thanks in advance,
There might be a better way but I know this works.

$cmd = "&'$testcmd' param1"
$executioncontext.InvokeCommand.invokescript($cmd)

As does this

$cmd = $executioncontext.InvokeCommand.NewScriptBlock("&'$testcmd'
param1")
$cmd.invoke()
Kiron
2008-04-24 15:41:58 UTC
Permalink
Single-quote the executable's path and escape the space(s):

$testcmd = 'C:\Program` Files\test\testutil'
$CMD = "$testcmd param1"
invoke-expression $CMD

--
Kiron
Bob Landau
2008-04-24 15:48:00 UTC
Permalink
Frank,

From what you've shown Invoke-Expression is an overkill. PS will do string
expansion for you nevertheless here are a few ways of doing it

## script "t 1.ps1"
param([string] $msg)

write-host $msg
## end of script "t 1.ps1"
$t = '.\x y\t 1.ps1'
& $t hello ## this in my opinion is the clearest and easiest
invoke-expression '& $t hello'
$s = 'powershell'
invoke-expression '& $t $s'
Hi,
I have a situation where I need to define the executable with the path to be
used with invoke-expression. It works with putting the path in there but
because the path has a space, an error becomes of it. The program path has a
$testcmd = "C:\Program Files\test\testutil"
$CMD = "$testcmd param1"
invoke-expression $CMD
The term 'C:\Program' is not recognized as a cmdlet, function, operable
program, or script file. Verify the term and try again.
When I use & in place of invoke-expression, I also get an error. Is there a
way around this other than putting the directory of the executable in the
path?
Thanks in advance,
Continue reading on narkive:
Loading...