PowerShell Genie

PowerShell: null value or missing property

Error 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Check lookup results and object properties before accessing a member that may be missing or null.

What it means

An expression evaluated to null and then something asked it for a member. Without strict mode a misspelled property quietly yields null, so the visible failure often lands a line or two after the real mistake. The lookup that was supposed to produce an object produced nothing.

Likely causes

  • A Get-* call matched no objects and returned nothing at all
  • The property name is misspelled, or it belongs to a different type than the one in the variable
  • The property exists on the object but was never retrieved, as with Get-ADUser attributes outside the default set
  • An index was taken from an array that turned out to be empty
  • A function assigned its result to a variable on one branch instead of emitting it

Check

1PS> $value -eq $null
2PS> $value | Get-Member
3PS> $Error[ 0 ] | Format-List * -Force

Run these checks before changing anything.

What these checks establish

Test for an absent result with $null on the left. An object with a missing property is a different condition from no object at all. Inspect the property names and the lookup that produced the value.

Fix

Capture the query result, then decide how zero, one or several matches should be handled. Inspect the exact object type before calling its method. Request nondefault AD attributes in the query. Use a documented strict-mode version during development when you want missing properties to fail near their source.

Verify: Test an empty lookup and a record with an empty or absent property. Each should produce the intended behavior instead of a misleading substitute value.

Reference

Microsoft Learn: set-strictmode

Continue
Genie guidePowerShell: null value or missing property

Check lookup results and object properties before accessing a member that may be missing or null.