How to dump output to csv file

rdefino
rdefino used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010
Commented:
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

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial