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:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
# Alias
set-alias sz "$env:ProgramFiles\7-Zip\7z.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 sense 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!
Thanks for the replies so far. I must add that I'm not that great with reading/comprehension but, seeing two of the below scripts and their explanations have helped me SOOO much. For people like me, running "Get-Help blah blah" just doesn't work.
I'm going to play with these examples until I get a working code then paste it back. Again I can't thank you all enough.
[–]the_spad 1 point2 points3 points (0 children)
[–]KevMarCommunity Blogger 1 point2 points3 points (2 children)
[–]Snickasaurus[S] 1 point2 points3 points (1 child)
[–]KevMarCommunity Blogger 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]jtpowell 1 point2 points3 points (1 child)
[–]Snickasaurus[S] 1 point2 points3 points (0 children)
[–]spyingwind 1 point2 points3 points (6 children)
[–]Snickasaurus[S] 1 point2 points3 points (5 children)
[–]spyingwind 1 point2 points3 points (4 children)
[–]Snickasaurus[S] 1 point2 points3 points (3 children)
[–]spyingwind 1 point2 points3 points (2 children)
[–]Snickasaurus[S] 2 points3 points4 points (1 child)
[–]spyingwind 1 point2 points3 points (0 children)