Skip to content
Dec 3 2008

Running a PowerShell Script with a Space in the Path

Like with any new technology, getting started with PowerShell was not without a few hiccups. One of the first issues I ran into was how to properly pass script locations to PowerShell so that it could actually run them.

It turns out that PowerShell is a bit finicky in how it locates the script to run. You basically have two options for getting PowerShell to find and run your scripts:

  1. Explicitly pass the full path of the script to PowerShell, or
  2. Add your script location to the PATH environment variable so that you can just pass the script name

As simple as those sound, there’s still a catch: if the path to your script contains a space, PowerShell won’t be able to find it and will throw an error, unless you use some special notation. For example, below I’m trying to run a PowerShell script where the path has a space in it:

powershell1

Enclosing the path in quotes works with your basic cmd.exe, but not in PowerShell. To get this to work in PowerShell, you need to use notation that follows this format:

“& ‘[full path to script]‘”

I told you the notation was special. So now I have this, which will run the script just fine:

powershell2

And there you have it. You can bet I’ve filed this one away permanently.

Similar Posts:

  • http://blog.andybarber.net/post/Running-a-PowerShell-Script-with-a-Space-in-the-Path.aspx Running a PowerShell Script with a Space in the Path

    RE: Running a PowerShell Script with a Space in the Path

    Running a PowerShell Script with a Space in the Path

  • http://agile-and-testing.chriss-baumann.de/2011/05/cruisecontrol-net-powershell-and-paths-with-spaces/ CruiseControl.NET, Powershell and paths with spaces | (Agile) Testing

    [...] very weird when using PowerShell, but I found a solution described by Dave Donaldson at his blog: Running a PowerShell Script with a Space in the Path, which worked quite well for [...]

  • http://agile-and-testing.chriss-baumann.de/2011/05/quicklinks-for-may-2011/ QuickLinks for May 2011 | (Agile) Testing

    [...] Running a Powershell Script with a Space in the Path [...]