Bob Dylan Bob Dylan - 7 months ago 97
PowerShell Question

Extracting contents of archived files in place using 7-Zip and PowerShell

I want to extract all *.Z files in place in their current directories (7-Zip support LZW/UNIX compression).

I can't figure out why this is not working. I know how to use PowerShell to recursively get a full path of a filename and directory in which file resides. I also know how to use 7-zip to extract a file in place. But trying to put these two together is not working. Here's what I have:

Get-ChildItem -Recurse *.Z | foreach ($_) {7z.exe e $_.FullName -o$_.Directory}

I also tried:

Get-ChildItem -Recurse *.Z | foreach ($_) {7z.exe e $_.FullName -o$($_.Directory)}


Get-ChildItem -Recurse *.Z | foreach ($_) {7z.exe e $_.FullName -o${_.Directory}}

This is getting the files I want by
, and
is the correct path (no space between the o and directory is how 7-zip expects the output directory). However, it keeps trying to output the file to the .Z path rather than the directory. I know it is right if I do

Get-ChildItem -Recurse *.Z | Select Directory

that is where I want it to go.

I assume the issue was having no space but not sure why it would get same directory. I even manually defined a variable and passed that with no problems.

So the issue is 7-zip saying no files to process when I want to output to same directory but not sure why, and it seems
variable not being passed but file name again which makes no sense. Sample output per file:

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21

Scanning the drive for archives:
1 file, 226 bytes (1 KiB)

Extracting archive: E:\Files\2014\more_cowbell\myfancyunixcompresseddocument.txt.Z
Path = E:\Files\2014\more_cowbell\myfancyunixcompresseddocument.txt.Z
Type = Z

No files to process
Everything is Ok

Files: 0
Size: 0
Compressed: 226

It works fine if I specify one specific output directory (no variable), but I want them extracted in place due to many subdirectories and conflicting file names between them.

I want to extract all *.Z files in place in their current directories (7-Zip support LZW/UNIX compression).


The -o parameter should be inside a doublequoted string with $($var) for variables:

Get-ChildItem -Recurse *.z | %{ 7z e $_.FullName "-o$($_.Directory)"}