How to export users with blank password to CSV

Hi,

I found a script to show all users who have a blank password. I have tried to export this to a CSV but the information in the export is not what is shown on-screen (see code below). On screen I see each username with True or False below it. In the CSV it has the word “Length” and then a list of numbers. What I’d ideally like to do is export all users who show as having a blank password (TRUE) to a CSV. Can anyone help please?

$results = Get-ADUser -Filter * -SearchBase "OU=test,DC=lan" | ForEach {
$_.SamAccountName
(new-object directoryservices.directoryentry "", ("domain\" + $_.SamAccountName), "").psbase.name -ne $null
Write-Host ""
}
$results | Export-Csv C:\PasswordReports\blankpasswords.csv -NoTypeInformation

https://social.technet.microsoft.com/Forums/en-US/102d13e7-0f3e-4832-a36b-788b460a7b4e/how-to-export-screen-information-to-csv?forum=winserverpowershell#bb64cdc9-d7ef-4378-889b-59a744179320

Thanks. And apologies for cross-forum posting. From the initial reply on the technet website I wasn’t expecting a further follow up.

But if anyone does know how to export the output I see on the screen to a file (without me having to copy and paste the screen contents), that would be helpful.

Get-Help Out-File
Get-Help Start-Transcript

I replied on the other forum too. Here’s another way to setup an object with 2 properties that works with export-csv:

Get-ADUser -Filter * -ResultSetSize 10 | foreach {
   [pscustomobject]@{
     SamAccountName = $_.SamAccountName
     BlankPassword = (new-object directoryservices.directoryentry "",
       ("domain\" + $_.SamAccountName), "").psbase.name -ne $null
     }
}


SamAccountName BlankPassword
-------------- -------------
user1                  False
user2                  False
user3                  False