# Export-CSV inconsistent handling null objects

**URL:** <https://forums.powershell.org/t/export-csv-inconsistent-handling-null-objects/18476>\
**Category:** PowerShell Help\
**Created:** [February 1, 2022, 6:06pm UTC](https://forums.powershell.org/t/export-csv-inconsistent-handling-null-objects/18476 "2022-02-01T18:06:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Michaels](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@David\_Michaels](https://forums.powershell.org/u/David_Michaels)\
**Post date:** [February 1, 2022, 6:06pm UTC](https://forums.powershell.org/t/export-csv-inconsistent-handling-null-objects/18476/1 "2022-02-01T18:06:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 1, 2022, 11:30pm UTC](https://forums.powershell.org/t/export-csv-inconsistent-handling-null-objects/18476/2 "2022-02-01T23:30:52Z")

</div>

David,  
Welcome to the forum. 👋🏽

Before we proceed - could you please go back, edit your question and fix the formatting of the code? You should format code, sample data or error mesasges as code here in the forum. So we are easily able to copy your code or sample data and reproduce your situation.  
Thanks in advance.

> [@Welcome to the new PowerShell Forums](https://forums.powershell.org/t/welcome-to-the-new-powershell-forums/7/6):
>
> How to format code in [PowerShell.org](http://PowerShell.org) forums. [Codeformatting\_1]

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/export-csv-inconsistent-handling-null-objects/18476/3 "2024-05-16T20:19:40Z")

</div>


