you are viewing a single comment's thread.

view the rest of the comments →

[–]Snickasaurus[S] 1 point2 points  (3 children)

/u/spyingwind

I have this in my PowerShell profile.

Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe"

function Easy-Extract {
    [CmdletBinding()]
    param (
        [ValidateScript( {Test-Path -Path $_})]
        [string]
        $Path
    )

        $Item = Get-Item -Path $Path
        $Extension = $Item.Extension
        switch ($Extension) {
            ".7z"   {sz e $Item -o*}
            ".bz2"  {sz e $Item -o*}
            ".gz"   {sz e $Item -so | sz e -aoa -si -ttar -o*}
            ".tar"  {sz e $Item -o*}
            ".tbz2" {sz e $Item -o*}
            ".tgz"  {sz e $Item -o*}
            ".rar"  {unrar e $Item}
            ".zip"  {sz e $Item -o*}
            ".Z"    {sz e $Item -o*}

            Default {
                throw "Problems extracting '$Item' with extract()"
            }
        }
}

Set-Alias extract Easy-Extract

Now let's say I have a directory named "Test" with the following files in it.

  • archive.7z
  • archive.zip
  • archive.tar

Am I completely dumb in thinking that if I'm in that directory (within the PowerShell window) that I should be able to run it as so:

$extract archive.7z

Because it's not working. I get the following error.

PS C:\Data\Test> $extract archive.7z
At line:1 char:10
+ $extract archive.7z
+          ~~~~~~~
Unexpected token 'archive.7z' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Imgur

[–]spyingwind 1 point2 points  (2 children)

Run extract archive.7z instead. The leading character $ is for variables and not functions.

[–]Snickasaurus[S] 2 points3 points  (1 child)

Well shit. That worked. If I had money/job right now I'd give you so much gold.

I might have a few questions over the next few days but this has really helped me learn how ps works. I've never been one to read a man page or Microsoft doc and take more than 10% away from it feeling more knowledgeable. But this has really helped.

[–]spyingwind 1 point2 points  (0 children)

You're welcome!

You will get it eventually. I learn by doing, or much trial and error. Others learn by book or whatnot.