Powershell Unrar script v2.0!

This one is a little more andvanced, as it “cleans up” after itself by deleting the .rar parent folder after successfully extracting it. Of course, use this at your own risk. It tends to be a little ruthless, considering there’s no going back as soon as the parent folder is deleted.

In other news: IT’S FRIDAY!

# # # # # # # # # # # # # # # # # # # #
#                                     #
# Unrar Shizzle v 2.0                 #
# www.fuffle.net                      #
#                                     #
# Unrar multiple arhives,             #
# through multiple subfolders         #
#                                     #
# Written by: Frank V                 #
# frank@fuffle.net                    #
#                                     #
# # # # # # # # # # # # # # # # # # # #

cls
$parent = "f:cachetemp"
$unrarred = "f:cacheunpacked"
$files = @()
$unrarpath = "C:Program FilesWinRARunrar.exe"

# Check unrar.exe default path.
if ((Test-Path -Path $unrarpath) -ne $true){
write-host "Unrar.exe not present in default unrar folder. Please verify"
break
}

# Recurse trough all folders, filter on .rar
Gci $parent -Recurse -Filter "*.rar" | % {
$files = $files + $_.FullName
}

# Start extract, using call parameter "&"
foreach ($f in $files) {
& "$unrarpath" x -y $f $unrarred

# In case of GREAT SUCCESS - delete rar parent folder
$folder = Split-Path -Path $f -Parent
if ($LASTEXITCODE -eq "0"){
Remove-item -Path $folder -Recurse
}
}

– F

Unrar archives with Powershell

Are you like me and have literally thousands of .rar archived material laying around on old disks?

It can be a pain in the lower region of the backside of your body to unrar all of those files without it being time consuming and boring as hell.

After some fiddlin’ around in PowerShell and some Googelin’, I  created this script to do the job for me. It uses unrar.exe, a commande line based extract tool to do the job, and plases the files in a directory of your choice. Just run the script, and it unrars all the archives it finds under the parent folder.

Unrar.exe can be downloaded from here.

Remember to change the directory to unrar.exe if you choose a different install dir.

Please feel free to comment if you have some way of improving or simplifying the code.

– F

(Based on the script of this awesome guy)

$parent = 'c:temprar'
$unrarred = 'c:tempunrar'
$files = @()

# Test to see if Unpackdir is present
if ((Test-Path -Path $unrarred) -ne $true)
 
# If not present, create Unpackdir
{md C:Tempunrar}


Get-ChildItem $parent -Recurse -Filter "*.rar" | % {

    # Recurse through all subfolders looking for .rar files only.

    $files = $files + $_.FullName
}

foreach ($f in $files) {

    # UnRAR the files. -y responds Yes to any queries UnRAR may have.

   C:unrarUnRAR.exe x -y $f $unrarred
}