-1

I am using this command

Where {$_.Extension -match "zip||rar"}

but need to use this command too

Where {$_.FullName -notlike $IgnoreDirectories}

Should I use a && or II as in

Where {$_.Extension -match "zip||rar"} && Where {$_.FullName -notlike $IgnoreDirectories}

or

Where {$_.Extension -match "zip||rar"} || Where {$_.FullName -notlike $IgnoreDirectories}

?

What I am trying to accomplish is to have every zip and rar file be extracted but I want to skip the extraction of the zip or rar files in some of my directories. What is the best solution for this?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
RadFox
  • 419
  • 1
  • 4
  • 17

1 Answers1

1

When in doubt, read the documentation.

Windows PowerShell supports the following logical operators.

  • -and Logical and. TRUE only when both statements are TRUE.
  • -or Logical or. TRUE when either or both statements are TRUE.
  • -xor Logical exclusive or. TRUE only when one of the statements is TRUE and the other is FALSE.
  • -not Logical not. Negates the statement that follows it.
  • ! Logical not. Negates the statement that follows it. (Same as -not)

Put both clauses in the same scriptblock and connect them with the appropriate logical operator. For a logical AND between the two clauses use -and:

Where-Object {
  $_.Extension -match "zip||rar" -and
  $_.FullName -notlike $IgnoreDirectories
}

For a logical OR between the two clauses use -or:

Where-Object {
  $_.Extension -match "zip||rar" -or
  $_.FullName -notlike $IgnoreDirectories
}

In your case it's probably the former.


Note that your regular expression zip||rar matches any extension due to the empty string between the two |. To match only items with the extension .rar or .zip remove one pipe: $_.Extension -match "zip|rar".

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Your suggestion isn't working as I am not getting an error "You must provide a value expression following the Unexpected token '$.FullName' in expression or statement." How might I resolve this error? I thought it's why I needed two separate where's. – RadFox Nov 12 '16 at 20:58
  • This seems to fix the expression issue, but will it work? Where {($_.Extension -match "zip||prd") -and ($.FullName -notlike $ignore)} – RadFox Nov 12 '16 at 21:12
  • That was supposed to be `$_`, not just `$`. No idea how the underscore went missing. Fixed. – Ansgar Wiechers Nov 12 '16 at 22:38
  • 1
    The parentheses aren't required, since the `-match` and `-notlike` operators have higher [precedence](https://technet.microsoft.com/en-us/library/hh847842.aspx) than the logical operators. Also, you still have the double pipe in the regular expression, so the expression will still match any extension. Other than that whether the condition will work depends on the value of `$ignore` and what you want it to exclude. – Ansgar Wiechers Nov 12 '16 at 22:41
  • Thanks, after my current process finishes, I'll try your suggestion and remove the parentheses. – RadFox Nov 13 '16 at 01:03