7-Zip Paths with Spaces in Windows PowerShell

Running 7-Zip commands via Windows PowerShell often triggers errors when file paths or the installation directory contain whitespace, such as C:\Program Files\7-Zip\7z.exe. Because PowerShell uses spaces to delimit commands and arguments, it misinterprets unquoted or improperly quoted path strings. This article explains how PowerShell parses whitespace in 7-Zip commands and provides practical methods—including the call operator, strategic quoting, and argument splatting—to ensure commands execute without errors.

The Whitespace Issue in PowerShell

By default, PowerShell interprets spaces as separators between commands, parameters, and arguments.

If you execute:

C:\Program Files\7-Zip\7z.exe

PowerShell attempts to execute C:\Program and treats Files\7-Zip\7z.exe as arguments, resulting in a CommandNotFoundException.

If you enclose the path in quotes to preserve the space:

"C:\Program Files\7-Zip\7z.exe"

PowerShell treats the input as a string literal and prints the text to the console instead of running the executable.

Solution 1: Use the Call Operator (&)

The standard way to run an executable path containing spaces is the call operator (&). It instructs PowerShell to treat the preceding quoted string as a command to be invoked.

& "C:\Program Files\7-Zip\7z.exe" a "archive.zip" "C:\Users\User\Documents\My File.txt"

In this syntax:

  • The executable path is enclosed in double quotes and preceded by &.
  • Any parameter or target file containing spaces is also wrapped in quotes.

Solution 2: Handling 7-Zip Switches with Spaces

7-Zip uses specific switches that require careful formatting when combined with spaces.

For instance, the output directory switch (-o) cannot have a space between the switch flag and the directory value. When the target directory contains spaces, wrap the entire switch or the path in quotes:

& "C:\Program Files\7-Zip\7z.exe" x "archive.zip" "-oC:\Extracted Files"

Alternatively:

& "C:\Program Files\7-Zip\7z.exe" x "archive.zip" -o"C:\Extracted Files"

Both formats prevent PowerShell from breaking the switch and path into separate parameters.

Solution 3: Argument Splatting and Arrays

When writing scripts, passing multiple paths with spaces can make command lines difficult to read. Defining arguments inside an array or using splatting simplifies the syntax. PowerShell automatically preserves the spaces within individual array items without requiring extra escape characters.

$7z = "C:\Program Files\7-Zip\7z.exe"
$arguments = @(
    "a",
    "-tzip",
    "C:\Backup Destination\backup.zip",
    "C:\Source Folder\Data Files\*",
    "-r"
)

& $7z $arguments

Solution 4: Adding 7-Zip to the System PATH

To eliminate the need to quote the executable path entirely, add the 7-Zip directory to the Windows PATH environment variable.

Once configured, call the binary directly by its name:

7z a "C:\Target Archive\data.zip" "C:\My Documents\file.txt"

While this removes the need for quotes around the executable name, any file, folder, or switch paths containing spaces must still remain enclosed in quotation marks.