Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Trying to find instance of item in array, -contains isn't working

$
0
0

I'm trying to write a script in Powershell that will match extensions of file names. This is what I have so far:

$location = $args[0];

[String]$extensions = @(".mkv", ".mp4", ".avi", ".wmv");
$pruneSize = 41240000;

Set-Location $location;

Get-ChildItem -Recurse | ForEach-Object {
    If($extensions -contains $_.Extension) {
        Write-Host "Found it";
    }
    else {
        Write-Host $_.Extension not found in $extensions;
    }
}

And here is the output:

.mp4 not found in .mkv .mp4 .avi .wmv
.bat not found in .mkv .mp4 .avi .wmv
.bat not found in .mkv .mp4 .avi .wmv

So it seems that it should work but it doesn't. If I switch "-contains" to "-match" then it works, though I don't want to use match as it seems that -contains should be what I really want and is more accurate for what I need. Am I missing some quirk of powershell?

Thanks!


Viewing all articles
Browse latest Browse all 6937

Trending Articles