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.