Powershell - Creating a blank CSV

Deano

I am trying to create a blank CSV file with headers only. I have the following code -

$headers = "Type", "Base", "Status"
$psObject = New-Object psobject
foreach($header in $headers)
 {
  Add-Member -InputObject $psobject -MemberType noteproperty -Name $header -Value ""
 }
$psObject | Export-Csv "d:\test.csv" -NoTypeInformation -Force

$now = get-date -Format "ddd MMM dd HH:mm:ss BST yyyy"
$status = "<!>Status,Last ran at $now"
$status >> "d:\test.csv" 

This creates the CSV but i get a blank line after the Headers and before the status. When I open the CSV I can see the following below the headers

"Type","Base","Status"
"","",""
< ! > S t a t u s , L a s t   r a n   a t   F r i   J u n   1 7   1 6 : 0 5 : 4 2   B S T   2 0 1 6 

I think "","","" is causing the blank lines because of -Value "". I tried -Value $null and still getting a blank line. If I remove value completely then I get prompted for a value.

How do I remove the blank line this creates?

Dave Sexton

Are you over thinking it? Can you not create an empty CSV like this:

 Set-Content "d:\test.csv" -Value "Type,Base,Status"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related