Forum Discussion

John_Dodo's avatar
John_Dodo
Copper Contributor
Jun 22, 2022
Solved

Use an arraylist in a comparison operation

Hello,   here is a short code.         $infos = $null $infos = @{} $infos = @{ "toto" = "tutu" ; "hello" = "hellu" <#...#> } $extensions = $null $extensions = New-Object -TypeName 'System...
  • LainRobertson's avatar
    Jun 22, 2022

    John_Dodo 

     

    Hey, John_Dodo .

     

    Yep, there sure is: it's the -in operator.

     

    For example:

     

    ($_.Extension -in @(".frm", ".ps1", ".txt"))

     

     

    Edited:

    In the context of your script (I answered a bit too hastily), you can drop lines 9 and 10, then change 11 to:

    $extensions = @(".txt", "cmd",".ps1", ".txt");

     

    And then line 18 to:

    Get-ChildItem -File -Path ".\Desktop\test" -Recurse | Where-Object { $_.Extension -in $extensions } |

     

    -File means you won't need the PSIsContainer test in the Where clause.

     

    Cheers,

    Lain

Resources