比较 Powershell 中的两个列表 [英] Compare two lists in Powershell

查看:51
本文介绍了比较 Powershell 中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 powershell.我有两个 132 和 134 条记录的列表.它们共有 85 条记录,我想获取单独列表中 list1 但不在 list2 中的值,比如 list_out1,以及在 list2 中但不在另一个列表中的 list1 中的值,比如 list_out2.我终于想打印 list_out1 和 list_out2.我尝试按照 这个答案 但它在尝试打印 list_out1 时给了我 list1 中的所有值.此外,我尝试使用 foreach 循环和 if 条件如下,它也给了我 list1 中的所有值来打印 list_out1.

I have just started working on powershell. I have two lists of 132 and 134 records each. They have 85 records in common and I want to get the values which are in list1 but not in list2 in a seperate list say list_out1 and the values which are in list2 but not in list1 in another list say list_out2. I finally want to print list_out1 and list_out2. I tried to do as given in this answer but it is giving me all the values in list1 while trying to print list_out1. Also, I have tried using foreach loop and if condition as below and it is also giving me all the values in list1 to print list_out1.

foreach ($i in $list1)
{
   if($list2 -notcontains $i) {
      $i
    }
}

我不知道我哪里做错了.这个逻辑对我来说似乎没问题.如果我错了,请纠正我.

I don't know where am I doing wrong. The logic seems to be alright for me. Correct me if I am wrong.

推荐答案

使用 Compare-Object 正是您所追求的.假设您执行 $List1.Item 或类似的操作.

Using the Compare-Object is what you are after. Assuming you do $List1.Item or something similar.

$MissingGroups = Compare-Object -ReferenceObject ($List1) -DifferenceObject ($List2) -Property Item | Where-Object{$_.sideIndicator -eq "<="}

这篇关于比较 Powershell 中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆