PowerShell Compare-Object doesn’t handle null values

I thought I’d run into a bug with the Compare-Object cmdlet in PowerShell version 5.1 earlier today.

Compare-Object : Cannot bind argument to parameter ‘ReferenceObject’ because it is null.
At line:1 char:33
+ Compare-Object -ReferenceObject $DriveLetters -DifferenceObject $Driv …
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

Running the same commands on a VM with PowerShell version 5.0 completed without issue so it initially appeared to be a problem with the Compare-Object cmdlet in PowerShell version 5.1.

The error was actually due to Get-Volume returning several results that didn’t have a drive letter on the system running PowerShell version 5.1.

There just happened to be no drives without letters on the VM that the results were being compared to. In other words, all drives had drive letters on the comparison VM. This explains why it succeeded without error on that particular system.

Once the entries without drive letters were filtered out on the system running PowerShell version 5.1, the command completed successfully.

I would like to thank both Jeff Hicks and Aleksandar Nikolic for their assistance in helping determine the source of the problem. I thought I’d share this problem because I’m sure it’s something that others will run into.



  1. Tony R Auby

    Interesting. And how does it work that you can insert a ‘Where’ at the point you do, as though it were a method to the Get-Volume cmdlet? (Get-Volume).Where({$_.DriveLetter}).DriveLetter

  2. David Bird

    Thank you so much for sharing this! I just ran in to this issue while trying to compare a csv export of users from an application to our users in Active Directory. We have a lot of AD users with no EmailAddress property.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: