0

In powershell I have this code:

$AddTags = @()
$AddTags = $AddTags + "ABC"
$AddTags = $AddTags + "abc"
$Object = @{
    "id"   = 1;
    "tags" = $AddTags
}
if($Object.tags -ne $AddTags)
{
    "Why is this?"
}

For some reason, $Object.tags doesn't equal $AddTags. Why i hover over $Object in VS code debug mode, I can see that $Object.tags contains a hashtable with one Entry, being the key "tags" and the value $AddTags. Why is that?

I have searched for this Problem and haven't found it anywhere.

2
  • 1
    .tags is not a Hashtable. $Object.tags.GetType() --> System.Array. Use Compare-Object to test if arrays have the same elements Commented Feb 21, 2023 at 11:16
  • If you are trying to add to that fixed array, you'd use +=; which isn't recommended to begin with. So, it would be: $AddTags += 'ABC'; $AddTags += 'abc' Commented Feb 21, 2023 at 17:20

1 Answer 1

0

Thanks, Theo. The problem here is that I didn't compare the arrays correctly. I learned how to do that here. Modifying the code like this works:

$AddTags = @()
$AddTags = $AddTags + "ABC"
$AddTags = $AddTags + "abc"
$Object = @{
    "id"   = 1;
    "tags" = $AddTags
}
$areEqual = @(Compare-Object $Object.tags $AddTags).Length -eq 0
if(!$areEqual)
{
    "This will no longer show up."
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.