Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaces cause split in path with PowerShell

Tags:

powershell

I'm having an issue with powershell when invoking an exe at a path containing spaces.

PS C:\Windows Services> invoke-expression "C:\Windows Services\MyService.exe"

The term 'C:\Windows' 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.

It seems to be splitting on the space between 'Windows' and 'Services'. Any idea how to get around this problem?

like image 420
jaffa Avatar asked Aug 30 '13 15:08

jaffa


People also ask

Do spaces matter in PowerShell?

Spaces around special charactersWhite-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code.

What does dot backslash mean in PowerShell?

Simply typing the name of the script is not enough. In this case, leading the script's name is a dot-backslash (". \"), which specifies to Powershell that the script to be run is in the current directory.

How do I echo in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.


1 Answers

Would this do what you want?:

& "C:\Windows Services\MyService.exe" 

Use &, the call operator, to invoke commands whose names or paths are stored in quoted strings and/or are referenced via variables, as in the accepted answer. Invoke-Expression is not only the wrong tool to use in this particular case, it should generally be avoided.

like image 124
Adil Hindistan Avatar answered Sep 29 '22 14:09

Adil Hindistan