Question:
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:
1 2 3 4 5 6 7 8 9 10 |
$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.?
Answer:
Use single quotes and a backtick (grave accent) to escape the space:
1 2 3 |
$path='E:\Test\My` space\Log' Invoke-Expression $path |
Or programmed:
1 2 3 |
$path="E:\Test\My space\Log" Invoke-Expression ($path -Replace ' ', '` ') |