mjzach22
mjzach22

Reputation: 29

Reading corresponding lines from 2 text files with 2 foreach() loops

Needing to pull 2 objects from 2 different txt files, 1 getting the user id and the other the rule id. The line numbers should correlate to each other from both text files. The issue is the 2 foreach() loop, the outer one runs once and the inner one run for each rule id in the txt, problem is I dont know the proper format to fix the issue. Please help.

$users = Get-Content -Path C:\Scripts\Inbox-Rules\user.txt
$rules = Get-Content -Path C:\Scripts\Inbox-Rules\UsersIDRule.txt

foreach ($user in $users) {
  foreach ($rule in $rules) {
    Remove-InboxRule -Mailbox "$user" -Identity "$rule"
  }
}

Upvotes: 1

Views: 96

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25021

If line numbers correlate with each other and align perfectly, then you can just access the line number of each array through their respective indices.

for ($i = 0; $i -lt $users.count; $i++) {
  Remove-InboxRule -Mailbox $users[$i] -Identity $rules[$i]
}

Upvotes: 3

Related Questions