Forum Discussion
John_Dodo
Jun 22, 2022Copper Contributor
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...
- Jun 22, 2022
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
LainRobertson
Jun 22, 2022Silver Contributor
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
- John_DodoJun 22, 2022Copper Contributor
Thank you Lain !!