Compare two hashtables

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Compare two hashtables

Post by alwo23@hotmail.com »

Hello,
i need help to compare two hashtables:

Hashtable 1:

$replace = @{}
$replace['l'] = "Kempten"
$replace['Description'] ="Held des Tages"
$replace['sn'] = "Proper"
$replace['StreetAddress'] = "Landstrasse"
$replace['wWWHomePage'] = "www.google.de"
$replace['mail'] = "Test@contoso.com"

Hashtable 2
$props = Get-ADUser -Filter { DisplayName -eq "Meister Proper" } -Properties *

$propkey = @{ }
$propkey['l'] = $props.l
$propkey['Description'] = $props.Description
$propkey['sn'] = $props.sn
$propkey['StreetAddress'] = $props.StreetAddress
$propkey['wWWHomePage'] = $props.wWWHomepage
$propkey['mail'] = $props.mail

$output = $Replace| Out-String
Write-Host $output

$output2 = $propkey | Out-String
Write-Host $output2

compare $replace.Values $propkey.Values

Result:

Hashtable 1
Name Value
---- -----
Description Held des Tages
wWWHomePage www.google.de
mail Test@contoso.com
l Kempten
StreetAddress Landstrasse
sn Proper

Hashtable 2
Name Value
---- -----
Description wwww
wWWHomePage
mail MProper@kempten.de
l Kempten
StreetAddress
sn Proper

Compare-Result

InputObject SideIndicator
----------- -------------
{wwww, $null, MProper@kempten.de, Kempten...} =>
{Held des Tages, www.google.de, Test@contoso.com, Kempten...} <=


I need now the different between the hashtables as a new hashtable as example:

Key Value
l Kempten

Thx a lot.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Compare two hashtables

Post by jvierra »

User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Compare two hashtables

Post by alwo23@hotmail.com »

Yes i know it, but i didnt have the result what i need.

I need a compare key by key and the different in a new hashtable with key an value filtert by SideIndicator.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Compare two hashtables

Post by jvierra »

You will have to write a custom script to do that with hashes.
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Compare two hashtables

Post by alwo23@hotmail.com »

Ok. Did you have an example or something else for me?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Compare two hashtables

Post by jvierra »

Here is how to compare hash tables for all keys:
  1. $replace = @{
  2.     l = "Kempten"
  3.     Description = "Held des Tages"
  4.     sn = "Proper"
  5.     StreetAddress = "Landstrasse"
  6.     wWWHomePage = "www.google.de"
  7.     mail = "Test@contoso.com"
  8. }
  9. $propkey = @{
  10.     l = '$props.l'
  11.     Description = '$props.Description'
  12.     sn = 'Proper'
  13.     StreetAddress = '$props.StreetAddress'
  14.     wWWHomePage = '$props.wWWHomepage'
  15.     mail= '$props.mail'
  16. }
  17.  
  18. Compare-Object ([PSCustomObject]$replace)  ([PSCustomObject]$propkey) -Property sn, description, StreetAddress
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Compare two hashtables

Post by alwo23@hotmail.com »

Ok i have tested it, but the result is:

l : Kempten
Description : Held des Tages
sn : Proper
StreetAddress :
wwwhomepage :
mail : MProper@kempten.de
SideIndicator : =>

l : Kempten
Description : Held des Tages
sn : Proper
StreetAddress : Landstrasse
wwwhomepage : www.google.de
mail : Test@contoso.com
SideIndicator : <=

There are some properties with the same value but what i need is the different
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Compare two hashtables

Post by jvierra »

Why do you think you need to do this?
User avatar
alwo23@hotmail.com
Posts: 28
Last visit: Tue Jun 06, 2023 2:43 am

Re: Compare two hashtables

Post by alwo23@hotmail.com »

I will write back the different to the active directory. Only the changed thinks a need to be update in the active directory.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Compare two hashtables

Post by jvierra »

You do not need to do that. Just use "Replace" with the wholehash. D will only update the fiekds that are different.

Set-AdUser userid -Replace $replace

That is all we ever need to do. Let AD do the comparison for you.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked