Powershell extracter function

Coming from a bash background I’m trying to learn Powershell to expand my IT knowledge. Moving around in PowerShell is starting to make sense but scripting is still very confusing. The following is part of my bash profile.

extract () { if [ -f $1 ] then case $1 in *.tar.bz2) tar xjf $1 ;; *.tar.gz) tar xzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar e $1 ;; *.gz) gunzip $1 ;; *.tar) tar xf $1 ;; *.tbz2) tar xjf $1 ;; *.tgz) tar xzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "Problems extracting '$1' with extract()" ;; esac else echo "'$1' is not a file, maybe a directory" fi } 

This is used to quickly extract archives of different types. So if I am surfing the web and download a compressed file to my ~/Downloads folder I can just enter the following into my terminal (which stays open 24/7).

extract ~/Downloads/TheFile.tar.gz 

…and it extracts in a directory named after the file at the same location. I want to do the same thing in my PowerShell profile while leveraging 7zip to do most of the work. Would anyone mind helping me piece this together?

I’ve been reading up on the switch statement. I’m assuming it would look similar to this.

 # ScriptName : extract.ps1 # Author : Me # Date : 20180904 # Purpose : Extract file given as $1 argument. # Requirements : 7zip # Tests if (-not (test-path "$env:ProgramFiles7-Zip7z.exe")) {throw "$env:ProgramFiles7-Zip7z.exe needed"} # Alias set-alias sz "$env:ProgramFiles7-Zip7z.exe" # function extract(){ if($_ -pathType leaf){$1} then switch -Wildcard -File $1 in *.7z sz x $1 -o* } 

I’ve been searching for days now trying to piece this together but it’s just not making senese and without knowing what powershell calls/refers to something I’m not sure where to correct my search string. Any and all help piecing this together is greatly appreciated!

submitted by /u/Snickasaurus
[link] [comments]

Leave a Reply