What I usually do is:
[String]::IsNullOrWhiteSpace($Val.ToString())
Or that:
[String]::IsNullOrEmpty($Val.ToString())
Or that:
$Val.ToString() -eq [String]::Empty
This often works just fine, because [System.DBNull]::Value.ToString() returns an empty string, so both [String]::IsNullOrWhiteSpace([System.DBNull]::Value) and [System.DBNull]::Value.ToString() -eq [String]::Empty evaluates to True.
Obviously, they are not logically equivalent, since your data may have legal empty strings or may be a data type that does not make sense as an empty string (for example, an integer). However, since you often want to treat DBNulls in the same way as empty lines and spaces only for spaces, this can be useful if you know your data well.
If you really want to know if this is a DBNull value, of course, use [DBNull]::Value.Equals($Value) .
Bacon bits
source share