Ying Li at myITforum.com

PowerShell & System Center

PowerShell Script to compare two arrays

Every now and then in my work, I need to compare excel sheets, like which clients managed by SMS also managed by opsware(or not). It is really a pain to compare those excel sheets line by line or search to filter hundreds out of thousands!

I wrote the below PowerShell script to make my life (now yours also) easier.

What it does is to create two arrays out of the two target lists. And then use the PowerShell containment operators to sort out the difference and write the results to text files.

Now you can cut down otherwise hours work to minutes. (Don’t let your boss know you have this tool – you could get a couple of hours break! )

$arrFirst = get-content First.txt
$arrSecond = get-content Second.txt

New-Item "C:\Myworkplace\ps\FirstInSecond.txt" -Type file
New-Item "C:\Myworkplace\ps\FirstNotInSecond" -Type file

Foreach ($First in $arrFirst)
{
  If ($arrSecond -contains $First)
         {Add-content "C:\Myworkplace\ps\FirstInsecond.txt" $First}
  Else
  {Add-content "C:\Myworkplace\ps\FirstNotInSecond.txt" $First}


 

Posted: Apr 19 2007, 04:31 PM by yli628 | with no comments
Filed under:

Comments

No Comments