When working on my Integrated PowerShell Help PowerPack for PowerGUI the other day I came across something that I haven’t quite figured out yet, so I thought I’d share it here and see if anyone else can contribute. Here’s the challenge:
(Get-PSSnapin snapinName) -eq (Get-PSSnapin -Registered snapinName)
Assuming that you’ve already added a snapin called snapinName to your PowerShell session using Add-PSSnapin snapinName, why doesn’t this expression return True?
Here are some of the reasons why it seems to me like this should return True:
- In both cases, you’re retrieving an object of type System.Management.Automation.PSSnapInInfo.
- If you pipe either side of the comparison into Select-Object and specify that you want to see all properties the output will be identical ( | Select-Object -Property *).
- If you pipe either side of the comparison into Format-Custom, the output will be identical.
- The PowerShell snapin is the same on both sides of the comparison.
- In one case you’re asking for a specific PowerShell snapin by name that has been added to the current PowerShell session and in the other case you’re asking for a specific registered PowerShell snapin by name; in both cases, you’re still asking for the same PowerShell snapin.
The only difference I was able to find between these two objects was that the hash code returned from the GetHashCode method was different.
If you know why this test for equality fails and want to share that knowledge, I’d appreciate it.
P.S. There is something else odd about PowerShell snapins that I encountered that is related to this. If you call Add-PSSnapin sNaPiNnAmE, PowerShell adds the snapin and stores it with the name just as you entered it (in this case, as sNaPiNnAmE). Why? PowerShell script isn’t case sensitive so the script can find the snapin just fine, but it doesn’t seem logical to add it to the PowerShell session using the same case you used in your script to add it when it could easily retrieve the name from the snapin object itself. This isn’t really important, but it is just something else I haven’t resolved that has been bothering me.
Technorati Tags: PowerGUI, PowerShell, PoSh, Poshoholic
3 thoughts on “PowerShell Challenge: Comparing PSSnapin objects”
The issue is that the object TYPE does not support COMPARISONs so the comparison is implemented by SYSTEM.OBJECT which does an object compare (e.g. are these the same instance of objects.). As you noticed, the hash is different which indicates that you have 2 objects. Even though they have the same properties, Object compare considers them not equivalent.
Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Thanks Jeffrey, that’s a great explanation! I knew it would be worthwhile posting this! 🙂
Also, just FYI dear reader, the solution to this problem is really simple. Just compare the Name property of the PSSnapin objects instead of comparing the objects themselves.
i.e. (Get-PSSnapin snapinName).Name -eq (Get-PSSnapin -Registered snapinName).Name