Testing for Database Null Values from PowerShell
PowerShell is super, but its documentation is still lousy. You might think that how to test a database field for a null value would be straightforward, and indeed it is, but it ain’t documented anywhere. I’ve just spent the afternoon searching and eventually had to work it out for myself. So here it is, with my compliments:
The class System.DBNull has a static field Value, which represents the sole instance of the class, so in effect [System.DBNull]::Value is the database null object. Like all objects, it has an Equals method. So [System.DBNull]::Value.Equals($DBField) returns true if $DBField contains a null.
How I use it is to declare the null object up front with script scope
$DBNull = [System.DBNull]::Value
and then use this wherever in the script I need to test for null, thus