• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer

Paul Cunningham

Author | Blogger | Online Trainer

  • Home
  • About
  • Blog
  • Books
  • Contact
You are here: Home / Code / Using Invoke-Expression in PowerShell With Spaces in Paths

Using Invoke-Expression in PowerShell With Spaces in Paths

Code · October 17, 2016

Share
Tweet
Share
+1
Stumble
Shares 0

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:

PowerShell
1
2
3
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:

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

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

PowerShell
1
2
3
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:

PowerShell
1
2
3
4
5
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.

Share
Tweet
Share
+1
Stumble
Shares 0

Filed Under: Code Tagged With: PowerShell

Paul Cunningham

Paul is a an author, blogger, and online trainer living in Brisbane, Australia. He's the founder of Practical 365, a Microsoft MVP, and a Pluralsight trainer. Say hi on Twitter.

Get my best career advice for IT professionals, straight to your inbox

* indicates required



Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Get my best career advice for IT professionals, straight to your inbox

* indicates required



Popular Posts

Career Advice for IT Professionals in 2018Career Advice for IT Professionals in 2018
T-Shaped People: Solving the Depth vs Breadth Skills DilemmaT-Shaped People: Solving the Depth vs Breadth Skills Dilemma
Book Review – Remote: Office Not RequiredBook Review – Remote: Office Not Required
Lessons from Running an Open Source Community ProjectLessons from Running an Open Source Community Project
Writing Technical BooksWriting Technical Books

Follow me on Facebook

Paul Cunningham

Latest Posts

  • A Short List of Things You Can’t Do
  • What Does Your Ideal Day Look Like?
  • Lessons from Running an Open Source Community Project
  • Book Review – Remote: Office Not Required
  • Career Advice for IT Professionals in 2018

Footer

Links

  • Home
  • About
  • Blog
  • Books
  • Contact

Recent Posts

  • A Short List of Things You Can’t Do
  • What Does Your Ideal Day Look Like?
  • Lessons from Running an Open Source Community Project
  • Book Review – Remote: Office Not Required
  • Career Advice for IT Professionals in 2018

Recent Comments

  • Paul Cunningham on Collect-ServerInfo.ps1 – A PowerShell Script for Windows Server Inventory
  • JimmyCap on Collect-ServerInfo.ps1 – A PowerShell Script for Windows Server Inventory
  • Udeme on Career Advice for IT Professionals in 2018
  • jdowg on Collect-ServerInfo.ps1 – A PowerShell Script for Windows Server Inventory
  • Paul Cunningham on Career Advice for IT Professionals in 2018

About Paul

Paul is a an author, blogger, and online trainer living in Brisbane, Australia. He's the founder of Practical 365, a Microsoft MVP, and a Pluralsight trainer. Say hi on Twitter. Read More…

Copyright © 2018 · Maker Pro on Genesis Framework · WordPress · Log in