Powershell hash containskey returns false incorrectly


I have a Powershell script which gets a list of Remote Desktop user sessions and puts them into a hash by SessionID.

I then can then dump the $userSessionByID in the PowerShell ISE

The frustrating part is that $userSessionBySessionID.ContainsKey(4) returns false. What am I missing here? I’ve tried $userSessionBySessionID.ContainsKey(“4”) as well, but that also returns false.


I think the issue may be that $userSession.SessionId.GetType() returns [UInt32]

In testing that is your issue exactly. Consider the following test where I create a hashtable using [UInt32].

Running $test.containskey(6) returns false as you have seen. I also get the same issue with $test.containskey("6"). However this returns true….

Note: You don’t need to use the -as operator here as you can just do a simple cast with [uint32]6 however if you are using variables that contain integers -as would be helpful.

The key itself can be near any object and containskey() is returning the correct result in all cases. Your hashtable does not have a key with the integer 6. Furthermore, [uint32] cannot be converted to [int] since it has a higher upper bound so a cast would not be possible. This would be a reason why PowerShell or the underlying .Net would not be doing an “automatic” cast. In practice I don’t think this ever happens in this scenario.

Point is be sure you are type aware.

Same example as above except this time we are using integers.

I can reverse the findings when I create the keys with strings.


Powershell hash containskey returns false incorrectly by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply