Export-Csv array of objects Error " Object reference not set to an instance of an object."

jeremy.hawkins1 0 Reputation points
2023-03-01T21:41:25.8566667+00:00

I am trying to make a powershell script that will read a text file split it by new lines then split each line by tabs and create an object then export the collection of objects into a csv file. I am using Export-Csv but it keeps erroring with the error about an Object Refrence not set.

this is the code that I am running that gives the error.

Filename is a parameter passed in to the script

This is my first time to really work with Powershell so please let me know if i am missing something simple.

$fileData = Get-Content -Row $filename
@(
  ForEach ($line in $fileData -Split "`n") {
    $values = $line -Split "\t"; @{
      ID             = $values[0]
      Acct           = $values[3]
      RootAcct       = $values[2]
      Term           = $values[4]
      Name           = $values[5]
      Code           = $values[6]
      Type           = $values[7]
      SIS            = $values[12]
  }
}
) | Export-Csv -Path ".\report.csv" -NoTypeInformatio
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-03-02T00:15:32.3433333+00:00

    -Row should be -Raw and you need to use a PSCustomObject.

    $fileName = ".\list.txt" 
    $fileData = Get-Content -raw $fileName               #row should be raw 
    @(
      ForEach ($line in ($fileData -Split "`n")) {
        $values = $line -Split "\t";
       [PSCustomObject]@{
          ID             = $values[0]
          Acct           = $values[3]
          RootAcct       = $values[2]
          Term           = $values[4]
          Name           = $values[5]
          Code           = $values[6]
          Type           = $values[7]
          SIS            = $values[12]
      }
    }
    ) | Export-Csv -Path ".\report.csv" -NoTypeInformation
    Get-Content .\report.csv
    
    
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2023-03-02T02:57:08.53+00:00

    There's no need to slurp in the entire content of the file by using the "-Raw" switch on the Get-Content cmdlet only to then create an array by splitting that string. Just use Get-Content and receive the file contents one line at a time.

    Also, by using that "foreach ($x in $y)" construct you've had to create an (another!) array to pipe the data to the Export-CSV.

    Learn to use the pipeline to save yourself the contortions needed to get the results you want. The code's easier to read, too.

    $fileName = ".\list.txt" 
    Get-Content $fileName |
        ForEach-Object {
            $values = $_ -Split "\t";
            [PSCustomObject]@{
                ID       = $values[0]
                Acct     = $values[3]
                RootAcct = $values[2]
                Term     = $values[4]
                Name     = $values[5]
                Code     = $values[6]
                Type     = $values[7]
                SIS      = $values[12]
            }
        } | Export-Csv -Path ".\report.csv" -NoTypeInformation
    Get-Content .\report.csv
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.