PowerShell批量创建Exchange Online收件箱规则报错求助
Let's break down the issues in your script and fix them step by step:
1. Correct the Array Definition
First, you've used @{} (a hashtable) to define your list of user emails, but you need a simple array of strings. Replace the curly braces with parentheses:
# Use @() for an array, not @{} (which is a hashtable) $names = @("name1@name.com","name2@name.com")
2. Fix the Folder Path Syntax Error
The error with $name:\Inbox happens because PowerShell interprets $name: as a drive identifier (like C: or D:), which isn't what you want here. To avoid this parsing issue, you have two reliable fixes:
Option 1: Use Subexpression to Construct the Path
Wrap the variable in a subexpression $($name) to tell PowerShell to fully resolve the variable before processing the rest of the path string:
foreach($name in $names){ Write-Host "Creating rule for $name" new-inboxrule -Name "MoveFromMailchimp" -Mailbox $name -MoveToFolder "$($name):\Inbox" -From "senderEmailAddress" -StopProcessingRules $false }
Option 2: Fetch the Inbox Folder Identity Explicitly
For more reliability (especially if folder names vary), use Get-MailboxFolder to retrieve the exact inbox identity for each user:
foreach($name in $names){ Write-Host "Creating rule for $name" # Get the inbox folder object for the user $inbox = Get-MailboxFolder -Identity "$name:\Inbox" # Use the folder's Identity property in the rule new-inboxrule -Name "MoveFromMailchimp" -Mailbox $name -MoveToFolder $inbox.Identity -From "senderEmailAddress" -StopProcessingRules $false }
Bonus: Clean Up the Parameter Syntax
Note that -StopProcessingRules: $false has an unnecessary colon and space—you can simplify it to -StopProcessingRules $false (PowerShell parameter syntax doesn't require the colon here).
内容的提问来源于stack exchange,提问作者HubDamian

