I am very new to powershell and am attempting to write a script that will move items from an outlook inbox into a 'Test' subfolder if the email subject matches a string. The script works in general however some emails are being skipped over.
The 1st time I run it, it moves 2 of the 4 emails that match the string. The 2nd time I run it, it moves the 3rd email. The 3rd time I run it, it moves the 4th email.
$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$targetfolder = $inbox.Folders | where-object { $_.name -eq "Test" }
$inbox.items | ForEach {
$a=$_.subject -like 'Test*'
if ($a) {[void]$_.Move($targetFolder)
}
}
I cant figure out why it will not move all the emails whose subject matches the 'Test*' string on the 1st time I execute it.
Any help would be much appreciated! Thank you