When I assign the variable $Users to the results of a Get-ADUser command, and it comes back with zero hits, I get a null variable that doesn’t error out in the code to export the results to csv, it just makes a blank csv file:
PS C:\Windows\system32> $Users = Get-ADUser -Filter {Name -notLike ‘Admin’ -and LastLogonDate -lt $InactiveDate -and Enabled -eq $true } -SearchBase $OU -Properties LastLogonDate, whenChanged | Select-Object @{ Name=“Username”; Expression={$_.SamAccountName} }, Name, LastLogonDate, whenChanged, DistinguishedName
PS C:\Windows\system32> ($users -eq $null)
True
PS C:\Windows\system32> $Users | Export-Csv C:\ADExports\InactiveCountyUsers.csv -NoTypeInformation
But when I do a op-add to add more users to the list with a different criteria, the returning results DO add to the variable when found, but when it finds nothing and the $Users variable stays null, it then errors when trying to export to csv?
PS C:\Windows\system32> $Users += Get-ADUser -Filter {Name -notLike ‘Admin’ -and LastLogonDate -notlike “*” -and whenChanged -lt $InactiveDate -and Enabled -eq $true} -SearchBase $OU -Properties LastLogonDate, whenChanged | Select-Object @{ Name=“Username”; Expression={$_.SamAccountName} }, Name, LastLogonDate, whenChanged, DistinguishedName
PS C:\Windows\system32> ($users -eq $null)
True
PS C:\Windows\system32> $Users | Export-Csv C:\ADExports\InactiveUsers.csv -NoTypeInformation
Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
At line:1 char:10
- $Users | Export-Csv C:\ADExports\InactiveUsers.csv -NoTypeInformation
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidData: ( [Export-Csv], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
What gives?