1

Tried many combinations using this script to compress a lot of files into individual files using 7zip, but I keep getting errors.

Get-ChildItem *.smc | ForEach-Object { 7z a -yx9 -x9 "$_.Name+.7z" $_.Name }

Keeps coming up with "Too Long Switch" or "Too Short Switch". What's going on here?

Jim Kieger
  • 861
  • 1
  • 7
  • 11

1 Answers1

3

There are two separate problems with your command:

  • You're incorrectly embedding property access $_.Name inside "...": you must use "$($_.Name)+.7z" - note the use of $(...), the subexpression operator.

    • In short: In order to embed expressions in an expandable string ("..."), you must enclose them in $(...). Notably, this includes property and indexed access (e.g., $($var.property), $($var[0])). Only variables as a whole do not require this (e.g., $var, $env:USERNAME). See this answer for more information.
  • Your options are meant to specify file-analysis levels and compression levels, which means that they must be passed as option arguments to the -m option.

    • Therefore, use -myx9 -mx9
mklement0
  • 312,089
  • 56
  • 508
  • 622