0

I try certain source codes using PowerShell to extract an password protected archive using 7zip:

This command doesn' work (7zip is an alias for $7zipPath):

& 7zip x "$zipFile" -o "$output" -p $zipFilePassword

I get the this error: Command Line Error: Too short switch:

But when I remove the spaces between the variables -o and -p, the archive can be extracted. This behaviour confuses me with other command line tools like git etc.? Why is it so?

Dennis Kassel
  • 2,726
  • 4
  • 19
  • 30
  • Does this answer your question? https://stackoverflow.com/questions/28160254/7-zip-command-to-create-and-extract-a-password-protected-zip-file-on-windows – Doug Maurer Oct 14 '20 at 00:38

1 Answers1

1

The behavior is specific to 7-Zip (7z.exe) and applies to whatever program (shell) you invoke it from:

Deviating from widely used conventions observed by CLIs such as git, 7z requires that even switches (options) that have mandatory arguments, such as -o and -p, have the argument directly attached to the switch name - no spaces are allowed:

& 7zip x $zipFile -o"$output" -p"$zipFilePassword"

Note that you normally need not enclose variable references in PowerShell in "..." (note how $zipFile isn't), even if they contain spaces. However, in order to attach them directly to switch names, you do.

Alternatively, you could enclose the entire token - switch name and argument - in double quotes:

& 7zip x $zipFile "-o$output" "-p$zipFilePassword"
mklement0
  • 382,024
  • 64
  • 607
  • 775