OK, look at the code:
AdUsers = new Collection<PSObject>();
Creates a new Collection, and assigns it to an external variable - either class level
public
/
private
/... or
static
AdUsers = PSCommandToRun("Get-ADUser -filter * -properties LastLogon, whenCreated, Enabled, LockedOut");
Discards the newly created collection and overwrites the variable with the return value.
So the
null
that is causing the problem is returned by the
PSCommandToRun
method, and nothing to do with dereferencing (which doesn't change variables at all!).
So start by looking at the
PSCommandToRun
method - use the debugger to look at exactly what it is trying to return, and why. That should give you clues - but you might also want to think "why am I returning the value of what is effectively a global variable anyway?"