Using Invoke-Expression in PowerShell With Spaces in Paths

One of the early bugs found in Exchange Analyzer was an error being thrown when the final HTML report was displayed. I had been developing the script in a folder path of X:\Scripts\ExchangeAnalyzer.  In other words, a path with no spaces. Some of the early users then tried to run the script from a path such as X:\Scripts\Exchange Analyzer and reported errors.

Here’s an example of what went wrong. Let’s say we’ve got an HTML file in C:\Scripts, and it’s called “Test Document.html”. If I use Invoke-Expression to launch that HTML file, an error will be thrown:

PS C:\Scripts> Invoke-Expression '.\Test Document.html'
.\Test : The term '.\Test' 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.

As it turns out, Invoke-Expression doesn’t handle spaces in paths like that. But there’s a couple of easy fixes that you can use.

One fix is to use the following command instead:

PS C:\Scripts> Invoke-Expression "& '.\Test Document.html'"

That will also work if the path to the document is stored in a variable.

PS C:\Scripts> $document = "C:\Scripts\Test Document.html"
PS C:\Scripts> Invoke-Expression "& '$document'"

The other option is to escape the spaces in the path, for example:

PS C:\Scripts> $document = "C:\Scripts\Test Document.html"
PS C:\Scripts> $document = $document -replace ' ','` '
PS C:\Scripts> Invoke-Expression $document

The first option is probably better, as it doesn’t require you to modify the existing variable or set a new variable for the path with escaped spaces. But either option should solve the problem of using Invoke-Expression with spaces in the path.

By Paul Cunningham

Paul is a writer and entrepreneur living in Brisbane, Australia. He enjoys spending time with his family and running in the mountains. Paul was the founder of Practical 365, a former Microsoft MVP, and Pluralsight trainer. Paul is also on Twitter and Instagram.

4 comments

  1. Hey,

    I know this article is old but maybe someone else will come across same issue I had.

    My issue was that I had a script path with spaces BUT also the script had parameters. So replacing the spaces was not enough as it messed with `iex` finding the parameters.. to fix I had to explore the world of splatting.. yes, splatting.

    “`
    $params = @{
    “param1” = “blah 1”
    “param2” = $true
    }
    $expression = “$base_path\generate.ps1”.Replace(‘ ‘, ‘` ‘)
    Invoke-Expression “$expression @params”
    “`

    1. Thanks for the comment! Been looking around for a solution to this and finally found this. My script for reference for others:
      $RunScript = “$PSScriptRoot\Clients\$Client\$RequestScript.ps1”
      $Parameters = @{File = $File}
      Invoke-Expression “. ‘$RunScript’ @Parameters”

      The value of $PSScriptRoot had spaces in it and was causing issues. Could have moved the script, but wanted something that worked in both situations :)

  2. Use this simple one liner: Invoke-Expression “C:\’Program Files (x86)\Microsoft Office\root\Office16’\EXCEL.EXE”

  3. does this same process applies to csv files.

    I am actually getting problems with
    Invoke-Expression “& ‘.\Test Document.csv'”

Comments are closed.