Re-importing csv results

this may be a powershell-adjacent question about CSVs.

i have a script, it queries AD, produces an arraylist, includes column ‘managedby’ and others. when the script is finished, i can query the “$arraylist | where managedby -eq $null” and it returns all the rows where there’s no value for that column. awesome.

but i know i’m going to want to query these results later, and the script takes a long time to run, and i don’t want to wait, so i $myarraylist | export-csv c:\file.csv the arraylist to a csv.

later when i $myarraylist = import-csv c:\file.csv the csv back in, there are no longer any results “where managedby -eq $null”. if i open the csv in excel, the cells look empty, and excel column filter sees them as ‘blanks’, but powershell sees something in there. " where managedby -like “*” " also returns every row in the list.

As long as the script runs you don’t need to save the $arraylist to a CSV file. But if you do you may look for .IsNullOrEmpty instead of only $null when re-importing the data. A CSV file is plain text.

You may try to save the $arraylist with Export-CliXML. I’d expect this way it should maintain the $nulls in it.

You may share your code here and we may try to improve it. :wink:

the script takes a long time to run, and i don’t want to wait.

#create a list with two rows
[System.Collections.ArrayList]$testArrayList= @()
$row = new-object psobject
$row | add-member -type NoteProperty -name 'GroupName' -Value "test"
$row | add-member -type NoteProperty -name 'Managedby' -Value $null

$row2 = new-object psobject
$row2 | add-member -type NoteProperty -name 'GroupName' -Value "test2"
$row2 | add-member -type NoteProperty -name 'Managedby' -Value "notnull"

#query original arraylist returns the correct row
$testArrayList | ? managedby -eq $null

#csv export/import/query  returns nothing
$testArrayList | export-csv C:\ftp.test\improve.csv
$testArrayListcsv = import-csv C:\ftp.test\improve.csv
$testArrayListcsv | ? managedby -eq $null

#xml export/import/query  returns correct row
$testArrayList | Export-Clixml C:\ftp.test\improve.xml
$testArrayListxml = Import-Clixml C:\ftp.test\improve.xml
$testArrayListxml | ? managedby -eq $null

you are correct that the clixml keeps the nulls. any idea what the non-null character is that gets put into the blank spots in the csv?

Hmmm … am I wrong or did you withhold the long running part of the script from us? :wink:

I’m not sure what you mean. CSV is plain text and I’d expect an empty string would be the text representation of null.