# Powershell Compare-Object

**URL:** <https://forums.powershell.org/t/powershell-compare-object/24167>\
**Category:** PowerShell Help\
**Created:** [May 22, 2024, 7:45pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167 "2024-05-22T19:45:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![PSNoob](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@PSNoob](https://forums.powershell.org/u/PSNoob)\
**Post date:** [May 22, 2024, 7:45pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/1 "2024-05-22T19:45:32Z")

</div>

I have 2 lists - List1 contains names from a query; List2 contains names from file directory. Trying to cleanup the directories that students are no longer with the college. So, I need names on List2 that are not on List1.

I’m using Compare-Object with PassThru parameter; but, I’m getting mixture of differences and some with similar names.

1. Does the lists have to be sorted?

2. Is there a **different** way to achieve that?

Thank you

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 22, 2024, 7:51pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/2 "2024-05-22T19:51:05Z")

</div>

Since you do not share any code and no sample data for us to recreate and understand your issue and to play around with the answers will be short:

1. No.
2. Probably.

> [@PSNoob](#):
>
> I’m getting mixture of differences and some with similar names.

What’s the issue with that?

---

<div class="post-metadata">

**Author:** ![PSNoob](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@PSNoob](https://forums.powershell.org/u/PSNoob)\
**Post date:** [May 22, 2024, 8:18pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/3 "2024-05-22T20:18:56Z")

</div>

How do I share data?  
List1 is a sql-cmd query

```auto
$Inactive = Invoke-Sqlcmd -Query "select upper(substring(Firstname,1,1) + LastName) from xyz.[dbo].[Students] where active=1;`"

```

List2 is

```auto
$Pdir = $Dir1, $Dir2 | Get-ChildItem -Directory | ForEach-Object {$_.Name.ToUpper()} 

```

Edit: The PS command to compare:

```auto
$Diff = Compare-Object $Pdir $Inactive -PassThru | Sort-Object Name 
Write-Output 'Diff:' $Diff

```

The subdirectories are created using first letter of firstname and lastname. So, for example, Joe Smith on List1 will be JSmith, with upper, JSMITH and List2 will have JSMITH (with ToUpper)

So, a student, for example Katie Holmes, that’s no longer in the college will not be on List1 but, will be on List2 as KHOLMES.

I just want KHOLMES. Problem in this case is Compare-Object doesn’t completely filters out. It’ll filter out some of the names but, still shows active ones, in this case, JSMITH.

Edit: I apologize if I’m still missing something. Please let me know where and how I can create sample data.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 22, 2024, 8:50pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/4 "2024-05-22T20:50:48Z")

</div>

> [@PSNoob](#):
>
> How do I share data?

Sample data … as plain text CSV for example … formatted as code!!!

Speaking of … please when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

> [@PSNoob](#):
>
> I’m still missing something.

You might …

> [@PSNoob](#):
>
> I just want KHOLMES. Problem in this case is Compare-Object doesn’t completely filters out. It’ll filter out some of the names but, still shows active ones, in this case, JSMITH.

You may (re-)read the help for

> **[Compare-Object (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.4)**
>
> The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method,...

With the parameters `-ExcludeDifferent` or `-IncludeEqual` you can adjust what you want to see. And you can use the property `SideIndicator` to filter whatever you need from the output.

And BTW: PowerShell is by default case insensitive. You don’t need to make the input strings all upper or lower case.

```auto
$DirectoryList = 
'kholmes',
'jsmith',
'JLennon',
'PMcCartney',
'GHarrison',
'RStar'

$UserList =
'JSmith',
'PMcCartney',
'rstar'

$Result = 
    Compare-Object -ReferenceObject $UserList -DifferenceObject $DirectoryList -IncludeEqual 

$Result

```

The output looks like this:

```auto
InputObject SideIndicator
----------- -------------
JSmith ==
PMcCartney ==
rstar ==
kholmes =>
JLennon =>
GHarrison =>

```

Now … if you like to filter …

```auto
$Result |
    Where-Object -Property SideIndicator -NE -Value '=='

```

and the output looks like this:

```auto
InputObject SideIndicator
----------- -------------
kholmes =>
JLennon =>
GHarrison =>

```

---

<div class="post-metadata">

**Author:** ![PSNoob](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@PSNoob](https://forums.powershell.org/u/PSNoob)\
**Post date:** [May 22, 2024, 9:40pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/5 "2024-05-22T21:40:12Z")

</div>

The only thing I noticed is different is my List1 and List2 results do not have the single quotes. I know I can add the quotes to the Invoke-Sqlcmd List1.

But, how do I include the single quotes on the PS Get-ChildItem for List2?

```auto
$Pdir = $Dir1, $Dir2 | Get-ChildItem -Directory | ForEach-Object {$_.Name.ToUpper()}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 22, 2024, 9:45pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/6 "2024-05-22T21:45:08Z")

</div>

🤔

The quotes indicate strings. If you have objects with properties you don’t need quotes.

```auto
$Pdir = 
    $Dir1, $Dir2 | 
        Get-ChildItem -Directory | 
            Select-Object -ExpandProperty Name

```

BTW: Do you notice the difference between the format of the code I posted and yours? 😏

---

<div class="post-metadata">

**Author:** ![PSNoob](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@PSNoob](https://forums.powershell.org/u/PSNoob)\
**Post date:** [May 23, 2024, 7:31pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/7 "2024-05-23T19:31:28Z")

</div>

Yes, I tried to change the code posts.  
I think there’s something wrong with the strings that I’ll need to look into. PsPad can’t even differentiate some of the outputs from Write-Host.  
Enjoy your summer, Olaf.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 23, 2024, 8:43pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/8 "2024-05-23T20:43:00Z")

</div>

> [@PSNoob](#):
>
> Yes, I tried to change the code posts.

It looks good now. Thanks. 👍🏼

> [@PSNoob](#):
>
> I think there’s something wrong with the strings that I’ll need to look into. PsPad can’t even differentiate some of the outputs from Write-Host.

I don’t know what that means. If you need further assistance you would need to elaborate more detailed.

BTW: `Compare-Object` is actually made to compare objects with properties - not strings. So you actually make your task harder than necessary when you convert powerful dot Net objects with properties to boring plain text strings. 😉

> [@PSNoob](#):
>
> PsPad can’t even …

I don’t have any experience with PsPad but the most used IDE for PowerShell code development is probably [VSCode](https://code.visualstudio.com/). You may give it a shot - it is free and well maintained and supported.

> [@PSNoob](#):
>
> some of the outputs from Write-Host.

… whatfor do you use `Write-Host`? You may read the following blog post to set your expectations about `Write-Host`

> **[Write-Host Considered Harmful](https://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/)**
>
> \[2023 Update\]   I’ve been meaning to update this for a while.  The advice in this post is no longer current.  A while ago we changed the implementation of Write-Host to being a wrapper on top of Wr…

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [May 23, 2024, 10:43pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/9 "2024-05-23T22:43:43Z")

</div>

> [@Olaf](#):
>
> You may read the following blog post to set your expectations about `Write-Host`

Here is a quote from the article you linked

> The advice in this post is no longer current.

I personally have no problem with using write-host… when it’s appropriate. Unless it’s a controller script, that is almost never.

---

<div class="post-metadata">

**Author:** ![PSNoob](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@PSNoob](https://forums.powershell.org/u/PSNoob)\
**Post date:** [May 24, 2024, 4:47pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/10 "2024-05-24T16:47:09Z")

</div>

Using write-host with Windows Powershell ISE to sort of view the results at each stage of the script. I’m new to PS1, is there a better way / tool to use during development?

---

<div class="post-metadata">

**Author:** ![PSNoob](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@PSNoob](https://forums.powershell.org/u/PSNoob)\
**Post date:** [May 24, 2024, 4:50pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/11 "2024-05-24T16:50:04Z")

</div>

Ok. I think I got it to where I can show the issue / results. Can’t seem to get the correct results.  
If I run the following

```auto
$DirectoryList = 
'kholmes',
'jsmith', #userlist
'JLennon',
'Knguyen'
'PMcCartney', #userlist
'GHarrison',
'RStar', #userlist
'KNutra' #userlist

$UserList =
'JSmith',
'PMcCartney',
'rstar',
'knutra'

$Result = 
    Compare-Object -ReferenceObject $UserList -DifferenceObject $DirectoryList #| Where-Object -Property SideIndicator -NE -Value '=='

$Result

```

I get the following results

```auto
PMcCartney
GHarrison
RStar
KNutra

InputObject SideIndicator
----------- -------------
kholmes =>           
JLennon =>           
Knguyen =>           
PMcCartney <=           
rstar <=           
knutra <=           

```

I was expecting

```auto
kholmes
JLennon
Knguyen
GHarrison

```

Is that not what Compare-Object should return?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 24, 2024, 6:03pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/12 "2024-05-24T18:03:18Z")

</div>

> [@PSNoob](#):
>
> Using write-host with Windows Powershell ISE to sort of view the results at each stage of the script. I’m new to PS1, is there a better way / tool to use during development?

It’s perfectly fine to use it during development. More professional would be to use `Write-Debug` or `Write-Verbose` though. With increasing exeprience you will know when it is ok to use. 😉 … don’t worry too much.

> [@PSNoob](#):
>
> Is that not what Compare-Object should return?

You’ve got a missing comma after the Vietnamese guy in your `$DirectoryList`!!! ☝🏼 😏 😏

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [June 23, 2024, 6:03pm UTC](https://forums.powershell.org/t/powershell-compare-object/24167/13 "2024-06-23T18:03:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
