title: Comparing Arrays in Powershell
body: <p>There is probably a simple way to do this, but I've been hitting my head against a wall for hours at this point. I'm trying to grab several user attributes out of AD, compare two of those attributes, and then modify them based on the differences. However since some users have null values for either their office or department fields which causes compare-object to fail, I have those going into other arrays with a -replace to get rid of the nulls, so my variables look like this:</p>
  
  <pre><code>$UserData = Get-ADuser -filter * -properties physicaldeliveryofficename,department | select samaccountname,physicaldeliveryofficename,department
  $Offices = $UserData.physicaldeliveryofficename -replace "^$","N/A"
  $Departments = $UserData.department -replace "^$","N/A"
  </code></pre>
  
  <p>So far so good, but when I loop through to compare values, I start to run into trouble. Looping through the users like this seems to be comparing every element to every other element:</p>
  
  <pre><code>Foreach ($user in $UserData.samaccountname) {
  Compare-object $offices $departments -includeqeual}
  </code></pre>
  
  <p>While not having a loop and using compare-object by itself gives accurate results, but then I'd need a loop to check for matches anyway.</p>
  
  <p>Assuming I just want to determine which users have matching office and department fields (and based off that do a pretty simple Set-ADUser command), how would I go about comparing the values without checking every element against every other element?</p>