We help IT Professionals succeed at work.

Issues with using powershell to write to csv

YZlat
YZlat asked
on
I have a script that needs to write to csv file. At first I just created a file with extension csv and wrote to is either tab or comma delimited values but then I would have trouble reading the csv from another script. If I used tab delimiters I had issues reading from file, if i used commas, the each row in csv would show up in the first column, and if i used both, comam and tab, commawould be written at the end of the column as part of the value

Here is the code I used:
$Headers="DateTimeStamp" + "`t" + "First" + "`t" + "Second" + "`t" + "Third"
$Headers| Out-File -Append -FilePath $LogFile -Encoding Unicode

$Values = $dtstamp + "`t" + $val1 + "`t""" + $val2 + """`t""" + $val3 + """"
$Values | Out-File -Append -FilePath $LogFile -Encoding Unicode

val2 and val3 need to have double quotes around them.

So I wanted to use export-csv cmdlet instead. the problem is that I understand how I can use export-csv to export datatable or data from a collection, but in my case i have to write line by line. So is it possible to use export-csv as an alternative to code above?
Comment
Watch Question

i think a good idea will be as the following :

1- build your table into memory
2- fill it with all data
3- export the final table

$my_table = @()


# the following code has to be done into loop to fill the table

$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member NoteProperty -Name ""DateTimeStamp" -Value  $dtstamp
$ReturnedObj | Add-Member NoteProperty -Name "first" -Value  $val1
$ReturnedObj | Add-Member NoteProperty -Name "Second" -Value  $val2
$ReturnedObj | Add-Member NoteProperty -Name "third" -Value  $val3
$my_table += $ReturnedObj
CERTIFIED EXPERT

Author

Commented:
What does NoteProperty stand for?
- NoteProperty: A property with a static value.
I like using Arrays for this, fatnasa2010 posted a rough approximation of how you can do this, but I prefer this variation which shows how you can work in a dummy foreach loop:

@MasterArray = @()

foreach ($Item in $Items)
{
	$TempArray = @()
	$TempArray = "" | Select DateTimeStamp, First, Second, Third
	
	$TempArray.DateTimeStamp = $dtstamp
	$TempArray.First = $val1
	$TempArray.Second = $val2
	$TempArray.Third = $val3
	
	$MasterArray += $TempArray
}

$MasterArray | Export-CSV -NoType "C:\export.csv" -Delimiter "`t"

Open in new window


This will export to CSV which has a tab delimiter `t
CERTIFIED EXPERT

Author

Commented:
fatnasa2010, I tried your solution and it wrote
#TYPE System.Management.Automation.PSCustomObject
to the csv file before writing everything else

But it stopped after I added -NoType switch to Export-Csv as chrismerritt suggested.


also I pull data from AD and then loop through it
foreach ($item in $ou.psbase.Children){
Write-Host $item.name
      ReturnedObj = New-Object PSObject
      $ReturnedObj | Add-Member NoteProperty -Name ""DateTimeStamp" -Value  $dtstamp
      $ReturnedObj | Add-Member NoteProperty -Name "first" -Value  item.name
      $ReturnedObj | Add-Member NoteProperty -Name "Second" -Value  $val2
      $ReturnedObj | Add-Member NoteProperty -Name "third" -Value  $val3
      $my_table += $ReturnedObj

}

$my_table | Export-Csv $MyFile

and instead of writign the value of $item.name I get "System.DirectoryServices.DirectoryEntry"

although it does give me correct output of $item.name to the screen


CERTIFIED EXPERT

Author

Commented:
chrismerritt, your solution give me the same issue
Does this work?

@MasterArray = @()

foreach ($Item in $ou.psbase.Children)
{
      $TempArray = @()
      $TempArray = "" | Select DateTimeStamp, First, Second, Third
      
      $TempArray.DateTimeStamp = $dtstamp
      $TempArray.First = "$($Item.Name)"
      $TempArray.Second = $val2
      $TempArray.Third = $val3
      
      $MasterArray += $TempArray
}

$MasterArray | Export-CSV -NoType "C:\export.csv"
CERTIFIED EXPERT

Author

Commented:
let me try. i am not optimistic though, since ($item).name didn't work before:(
CERTIFIED EXPERT

Author

Commented:
"$($Item.Name)" worked! and so did "($Item.Name)"
Thank you!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.