Yet another empty pipe element error trying to export-csv

This topic contains 3 replies, has 3 voices, and was last updated by  Richard Diphoorn 1 year, 5 months ago.

  • Author
  • #37456

    Bob King


    Following the thread "another-empty-pipe-error-when-export-csv-used/#post-10028", I tried to use Dave Wyatt's second option of "force a foreach loop to pipe its results somewhere (placing the loop into a subexpression, and piping the results of the subexpression to the next command)" and derived the following to see which of our organizations servers are no longer in DNS. Unfortunately all I get is the screen display (helpful) and an empty file (not so much.):

            $Names = import-csv -Path "C:\temp\AD_servers.csv" | % {$_.Name}
    Foreach ($name in $Names)
        $Addresses = $null
        try {
            $Addresses = [System.Net.Dns]::GetHostAddresses("$Name").IPAddressToString
        catch { 
            $Addresses = "Server IP cannot resolve."
        foreach($Address in $addresses) {
            write-host "$Name,"$Address
    ) |export-csv "C:\temp\result.csv"


    -Bob King

  • #37457

    Richard Diphoorn

    Try this:

    $Names = Import-Csv -Path 'C:\temp\servers.csv' | ForEach-Object -Process {$_.Name}
        Foreach ($name in $Names) {
            $Addresses = $null
            Try {$Addresses = [System.Net.Dns]::GetHostAddresses("$name").IPAddressToString}
            Catch {$Addresses = 'Server IP cannot resolve.'}
            Foreach($Address in $Addresses) {
                [pscustomobject] @{
                    Name      = $name
                    IPAddress = $Address
    ) | Export-Csv -Path 'C:\temp\result.csv' -NoTypeInformation

    This works because I'm passing an object to Export-Csv. You used Write-Host; this only puts text in the console. Nothing else. So there is no input for Export-Csv.

  • #37458

    Bob McCoy

    Richard's correct. Read the OUTPUTS section of the Write-Host help file. It tells you that it empties the pipeline, that their is nothing left after that.

  • #37461

    Richard Diphoorn

    And I forgot to mention, every time you use Write-Host, and I'm quoting Don Jones, you kill a puppy. And we don't want to kill puppy's, don't we? 🙂

You must be logged in to reply to this topic.