Code Re-use: $AttVar and $AllUsers should have same OBjs. No need to do another lookup
For example, There is another bit, in the section where you loop through all the user accounts and do an "get-aduser [name] -properties *" on a per account basis. But... you already have the information in the $Allusers variables, and in fact on the 'foreach' $User variable you have, so you could pull that information out too without lookin anything up. Look around line 685ish:
Foreach ($User in $AllUsers) {
$AttVar = get-aduser -filter {name -eq $User.Name} -Properties * | Select-Object Enabled,PasswordExpired, PasswordLastSet, PasswordNeverExpires, PasswordNotRequired, Name, SamAccountName, EmailAddress, AccountExpirationDate, @{ Name = 'lastlogon'; Expression = { LastLogonConvert $_.lastlogon } }, DistinguishedName
If you think about it, $User already has the result of this so you should be able to do something like this: $AttVar = $User | Select-Object Enabled,PasswordExpired, PasswordLastSet, PasswordNeverExpires, PasswordNotRequired, Name, SamAccountName, EmailAddress, AccountExpirationDate, @{ Name = 'lastlogon'; Expression = { LastLogonConvert $_.lastlogon } }, DistinguishedName
I believe I did something to change this in my initial changes.
However, could likely remote $AttVar all together and change out $AttVar.Foo for $User.Foo -- I think the results would be the same, as $User already includes all of the necessary attributes; you're just calling them.