Avatar of Isaiah Melendez
Isaiah Melendez
 asked on

Powershell 7zip extract and Move

Dear Experts,

Could you provide an example of extracting .7z files with 7zip and moving the extracted all .7z files to a network directory?

.7z directory = C:\source

move extracted files directory = \\servername\c$\destination

Doing all this via powershell 3.0?
Powershell

Avatar of undefined
Last Comment
Zoltan Baksa

8/22/2022 - Mon
Greg Besso

I haven't used 7zip myself from within PowerShell (yet) but it seems you do have to do some special steps for that. Check this article out for some example of how to call it...

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-and-7Zip-83020e74

From there on it's just Move-Item (or Get-ChildItem -Recurse "somewhere" | Move-Item "toSomewhereElse". Check this out for that...
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/move-item

Let me know if that helps?
oBdA

This will expand the files directly to the target folder:
Function Expand-Archive([string]$Path, [string]$Destination) {
	$7z_Application = "C:\Program Files\7-Zip\7z.exe"
	$7z_Arguments = @(
		'x'							## eXtract files with full paths
		'-y'						## assume Yes on all queries
		"`"-o$($Destination)`""		## set Output directory
		"`"$($Path)`""				## <archive_name>
	)
	& $7z_Application $7z_Arguments 
}

Get-ChildItem "C:\source" -Filter *.7z | ForEach-Object {
	Expand-Archive -Path $_.FullName -Destination "\\servername\c$\destination"
}

Open in new window

Isaiah Melendez

ASKER
ObDA,

Your solution is 99% there. The only part I am missing was actually moving/deleting the files from the source location. So basically, if test1.7z and test2.7z are in the c:\source dir I want them to get deleted once the extract has been done into the \\servername\c$\destination location.

Does that make sense?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Greg Besso

Hey there sj77, use Remove-Item * -Include *.7z or something similar to do that.

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/remove-item
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Isaiah Melendez

ASKER
Greg,

Like this?

Function Expand-Archive([string]$Path, [string]$Destination) {
	$7z_Application = "C:\Program Files\7-Zip\7z.exe"
	$7z_Arguments = @(
		'x'							## eXtract files with full paths
		'-y'						## assume Yes on all queries
		"`"-o$($Destination)`""		## set Output directory
		"`"$($Path)`""				## <archive_name>
	)
	& $7z_Application $7z_Arguments 
        Remove-Item * -Include *.7z
}

Open in new window

Greg Besso

oBdA's response is very much on point and thorough sj77. I want to keep that in my toolbox going forward too :P
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zoltan Baksa

Hello guys,

can you please help me how to use the Expand-Archive function if a 7z file is password protected? Of course, i know the password :)

Thanks in advanced for your help.

Best regards,
Zoltan