6

I have a query like when I try to execute the expression which contains a path with space, I am getting an error as below.

Code:

$path="E:\Test\My space\Log"
Invoke-Expression $path 

E:\Test\My: The term 'E:\test\My' 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.
At line:1 char:1
+ E:\Test\My space\Log
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (E:\Test\My :String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Could you please help me to fix this issue.?

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Bharathi
  • 61
  • 1
  • 4

2 Answers2

3

Use single quotes and a backtick (grave accent) to escape the space:

$path='E:\Test\My` space\Log'
Invoke-Expression $path

Or programmed:

$path="E:\Test\My space\Log"
Invoke-Expression ($path -Replace ' ', '` ')
iRon
  • 20,463
  • 10
  • 53
  • 79
  • This doesn't address the problem which is a fundamental understanding issue – Maximilian Burszley Aug 03 '17 at 17:48
  • 2
    You are right, there is probably a fundamental understanding issue here but it does answer the question: *"Could you please help me to fix this issue.?"* – iRon Aug 03 '17 at 18:35
0

You should be able to just call

& $path

I think Invoke-Expression basically just types your value into the shell at face value.

Sambardo
  • 714
  • 2
  • 9
  • 15