How to dump output to csv file

I have the script below that runs fine, but I cannot get it to dump to a csv file. the output file is created, but it's empty. What am I missing?

Get-Content E:\rdefino\verify_alias\aliases_list.txt | % {$proxy = "smtp:"+$_.tostring().trim(); if(Get-ADObject -Filter {proxyaddresses -eq $proxy} ) {Write-Host "Found: $proxy"} else {Write-Host "Not Found: $proxy"}} | Export-Csv  E:\rdefino\verify_alias\alias_output.csv
rdefinoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
Write-Host does not send output to the output pipeline, it's for your consumption only.

Lose Write-host and think a bit about what you want to export. At the moment you're writing very simple strings, there's nothing to convert to a CSV. For example, you might do this instead.
Get-Content E:\rdefino\verify_alias\aliases_list.txt | ForEach-Object {
    $dn = ""
    $state = 'Not found'
    $proxy = "smtp:$_".Trim()
    if ($adObject = Get-ADObject -Filter { proxyAddresses -eq $proxy }) {
        $dn = $adObject.DistinguishedName
        $state = 'Found'
    }
    [PSCustomObject]@{
        Proxy = $_
        State = $state
        DN    = $dn
    }
} | Export-Csv  E:\rdefino\verify_alias\alias_output.csv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
First of all, I formatted your script:
Get-Content E:\rdefino\verify_alias\aliases_list.txt |
  % {
    $proxy = "smtp:"+$_.tostring().trim()
    if(Get-ADObject -Filter {proxyaddresses -eq $proxy})
    {
      Write-Host "Found: $proxy"
    } else {
      Write-Host "Not Found: $proxy"
    }
  } | Export-Csv  E:\rdefino\verify_alias\alias_output.csv

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Chris is correct.
If you really want the text output only, use Write-Output instead of Write-Host, and Out-File instead of Export-CSV.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.