systax error

Glenn Maxwell 7,561 Reputation points
2022-04-13T23:13:49.553+00:00

Hi All

i am executing the below syntax and i am getting error. experts guide me

$mailboxes = Import-Csv c:\temp\users.csv
foreach($mailbox in $mailboxes)
Search-Mailbox -Identity $mailbox.Name -SearchQuery {kind:meetings AND From:"user2@contos.com" AND Subject:"Test1 + Test2" AND (received>="03/03/2022 00:00" AND received<="03/04/2022 23:59")} -DeleteContent -Force
}

At line:2 char:32

  • foreach($mailbox in $mailboxes)
  • ~
    Missing statement body in foreach loop.
    At line:5 char:1
  • }
  • ~
    Unexpected token '}' in expression or statement.
  • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
  • FullyQualifiedErrorId : MissingForeachStatement
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
3,374 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
6,527 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
1,418 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kael Yao-MSFT 26,556 Reputation points Microsoft Vendor
    2022-04-14T02:08:56.337+00:00

    Hi @Glenn Maxwell

    The cause is that you are missing a { in this part:

    foreach($mailbox in $mailboxes)   
    Search-Mailbox -Identity $mailbox.Name  
    

    The whole script should be:

     $mailboxes = Import-Csv c:\temp\users.csv  
     foreach($mailbox in $mailboxes)  
     {  
     Search-Mailbox -Identity $mailbox.Name -SearchQuery {kind:meetings AND From:"user2@contos.com" AND Subject:"Test1 + Test2" AND (received>="03/03/2022 00:00" AND received<="03/04/2022 23:59")} -DeleteContent -Force  
     }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful