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?
Spaces around special charactersWhite-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With