What Im doing wrong?

Hello!

I have created a script that should sort specific e-mails into different folders. I have an array called $searchwords with all the e-mail adresses and the script works well exept that if for some reason skips the first mail in my mail folder and copies it to the Unknown folder. So for some reason the script goes to the “else” in the first loop… Anyone know why?? If I remove the else code the script run without any problem.

Folder structure:

  1. Mail (Contains alot of mail files with the file extension .mml)
  2. WantedMail (Directory that will get all the sorted emails)
  3. Script.ps1

Here is the code:

$mailfolder = “Mail”
$copiedmail = “WantedMail”
[int]$loop = 1

$searchwords = @(“test@mail.com,"test1@mail.com")

foreach ($x in (cmd /c dir $mailfolder /b))
{
    Write-host $loop -ForegroundColor Yellow
    $loop++
    foreach ($word in $searchwords)
    {
        if (Get-Content -Encoding UTF8 $mailfolder\$x -ReadCount 1000 | Where-Object {$_ -match $word})
        {
        Write-host $word -ForegroundColor green
            if(!(Test-Path -Path $copiedmail\$word))
            {
                New-item -ItemType Directory -Path "$copiedmail\$word"
                Copy-item -Path "$mailfolder\$x" "$copiedmail\$word" -Force
            }
            elseif (Test-Path -Path $copiedmail\$word)
            {
                Copy-item -Path "$mailfolder\$x" "$copiedmail\$word" -Force
            }

        }
        else
        {
            if(!(Test-Path -Path "$copiedmail\Unknown"))
            {
                New-item -ItemType Directory -Path "$copiedmail\Unknown"
                Copy-item -Path "$mailfolder\$x" "$copiedmail\Unknown" -Force
            }
            Write-host "$word was not found in $x" -ForegroundColor Red
        }
    }
}

You’re using Get-Content which will look into a file for info. Since you’ve got tons of files that you’re matching based on names, you should use Get-ChildItem, which will list out files.

You have multiple search words, but you’re copying the file the first time through your foreach ($word in $searchwords) loop without checking to see if the second word is in it. Based on what I can see of your code, it’s possible that the file that’s being “skipped” contains test1@mail.com but not test@mail.com . It’s also possible that the file is not UTF8 encoded, which would mess up your code.

I’ve revised your code slightly so it checks each file for each word, and only copies the file to Unknown if no matches were found. I also ditched Get-Content and Where-Object in favor of using Select-String instead, which is faster:

$mailfolder = "Mail"
$copiedmail = "WantedMail"
[int]$loop = 1

$searchwords = @("test@mail.com","test1@mail.com")

foreach ($x in (cmd /c dir $mailfolder /b))
{
    Write-host $loop -ForegroundColor Yellow
    $loop++
    
    $foundMatch = $false
    
    foreach ($word in $searchwords)
    {
        if (Select-String -Path $mailfolder\$x -Pattern $word -Quiet -Encoding UTF8 -SimpleMatch)
        {
            $foundMatch = $true
            Write-host $word -ForegroundColor green
            
            if(!(Test-Path -Path $copiedmail\$word))
            {
                New-item -ItemType Directory -Path "$copiedmail\$word"
            }
            
            Copy-item -Path "$mailfolder\$x" "$copiedmail\$word\" -Force
        }
        else
        {
            Write-host "$word was not found in $x" -ForegroundColor Red
        }
    }
    
    if (!$foundMatch)
    {
        if(!(Test-Path -Path "$copiedmail\Unknown"))
        {
            New-item -ItemType Directory -Path "$copiedmail\Unknown"
        }
    }
    
    Copy-item -Path "$mailfolder\$x" "$copiedmail\Unknown\" -Force
}

Thank you Stephen Owen for the tips and thank you Dave Wyatt for the help with the script. It work’s perfect!