Path With Spaces in PowerShell

Rohan Timalsina Jan 30, 2023
  1. Use Single Quotes ' ' to Deal With Spaces in the Path in PowerShell
  2. Use & Operator to Deal With Spaces in the Path in PowerShell
  3. Use " " Double Quotes to Deal With Spaces in the Path While Changing the Current Directory in PowerShell
Path With Spaces in PowerShell

A path is the complete location of a file or folder in the computer. In PowerShell, you will need to use the path to change the current working directory or run some files at the specified location.

Sometimes, when using the path in PowerShell, you might face an error if there are spaces because the space causes a split in the path. As a result, the command will not execute and returns an error in the output.

This tutorial will introduce different methods to deal with spaces in the path in PowerShell.

Use Single Quotes ' ' to Deal With Spaces in the Path in PowerShell

When you are running the command using Invoke-Expression, you can enclose the path of sub-directories with ' ' single quotes to deal with spaces in the path in PowerShell.

Without the ' ' quotes, spaces cause a split in the path and PowerShell will end the command when it finds a space in the path.

Invoke-Expression "C:\New Folder\script files\myscript.ps1"

Output:

Invoke-Expression command error

As you can see, PowerShell does not run the complete command because the path contains spaces. Notice what happens if we use ' ' quotes while running the file having spaces in the path.

For example:

Invoke-Expression "C:\'New Folder\script files'\myscript.ps1"

Output:

The myscript.ps1 contains the Get-Date cmdlet, which prints the current date and time.

Sunday, February 27, 2022 10:24:06 PM

The script file is executed without errors, so you can use this method to deal with spaces in the path.

Use & Operator to Deal With Spaces in the Path in PowerShell

The & operator runs the pipeline before it in the background. You can use the & operator to invoke commands by enclosing the path with spaces in double quotes " ".

& "C:\New Folder\script files\myscript.ps1"

Output:

Sunday, February 27, 2022 10:37:21 PM

Or, you can also use the . operator to run the command using the double quotes " " in the path.

. "C:\New Folder\script files\myscript.ps1"

Output:

Sunday, February 27, 2022 10:37:21 PM

Use " " Double Quotes to Deal With Spaces in the Path While Changing the Current Directory in PowerShell

The cd command changes the current directory in PowerShell. If the path contains spaces, you will get an error when you don’t put the path in double quotes " ".

For example:

cd Work Documents

Output:

Set-Location : A positional parameter cannot be found that accepts argument 'Documents'.
At line:1 char:1
+ cd Work Documents
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

You can see that running the cd command without " " gives an error while changing the path. Look at what happens when using double quotes " ".

cd "Work Documents"

Output:

As you can notice, the current working directory is changed to the specified path.

PS C:\Users\rhntm\Work Documents>

We hope this article helped understand why spaces cause a split in the path and deal with them in PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Path