Powershell script to unzip tar.gz to tar to content with 7zip and directory structure intact

Powershell script to unzip tar.gz to tar to content with 7zip and directory structure intact

$7z='C:\Program Files\7-zip\7z.exe'
$folder="YOURTAR.GZ.LOCATION"
$gzfiles=Get-ChildItem -Path $folder -Include *.gz -Recurse
foreach ($1stObject in $gzfiles) {
$1stparentfolder= [System.IO.Path]::GetDirectoryName("$1stObject")
& $7z x $1stObject -aos "-o$1stparentfolder"
$tarfiles=Get-ChildItem -Path $folder -Include *.tar -Recurse
foreach ($2ndObject in $tarfiles) {
$2ndparentfolder= [System.IO.Path]::GetDirectoryName("$2ndObject")
& $7z x $2ndObject -aos "-o$2ndparentfolder"
}
}