2

I have created custom data object that I output to csv. It all works fine, but how do I strip the header row from the output file? I've tried some variations of the "Select -Skip", but that usually generates a blank csv file. Here is the relevant bits of code:

#populate data row with user properties
$DataObject | Add-Member -MemberType NoteProperty -Name 'EventID'-Value $EventID 
$DataObject | Add-Member -MemberType NoteProperty -Name 'Computer'-Value $env:COMPUTERNAME 
$DataObject | Add-Member -MemberType NoteProperty -Name 'EngineerID' -Value $EngineerID
$DataObject | Add-Member -MemberType NoteProperty -Name 'UserID' -Value $UserID
$DataObject | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $DateTime
$DataObject | Add-Member -MemberType NoteProperty -Name 'StepCode' -Value $StepCode
$DataObject | Add-Member -MemberType NoteProperty -Name 'Result' -Value $Result
$DataObject | Add-Member -MemberType NoteProperty -Name 'Status' -Value $HomeDirectoryAccess

#export the dataobject to a CSV file
$OutputFile = ($OutFilePath + "\Status_" + $env:COMPUTERNAME + "_" + $DateStamp + "-" + $TimeStamp + ".csv")
$DataObject | Export-Csv -Path $OutputFile -NoTypeInformation

The $DataObject looks like this:

EventID    : Test
Computer   : MD-760R102
EngineerID : Jez
UserID     : laijer
DateTime   : 25/03/2015 12:24 PM
StepCode   : USH
Result     : Complete
Status     : Home Drive Access Allowed

I've also tried this code to output the data, but it puts all the data into the 1st cell of the csv file instead of individual cells across a single row:

$DataOutput = $DataObject.EventID + "," + $DataObject.Computer + "," + $DataObject.EngineerID + "," + $DataObject.UserID + "," + $DataObject.DateTime + "," + $DataObject.StepCode + "," + $DataObject.Result + "," + $DataObject.Status
$DataOutput | Out-File $OutputFile

1 Answer 1

3

You should be able to use ConvertTo-CSV and Select -Skip 1 together to get what you want. You'll have to split on new lines after converting to CSV, since it outputs a multi-line string, but that's simple enough.

($DataObject | Select EventID,Computer,EngineerID,UserID,DateTime,StepCode,Result,Status | ConvertTo-Csv -NoTypeInformation).split("`n") | select -skip 1 | set-content $OutputFile

That will give you the following:

"Test","MD-760R102","Jez","laijer","25/03/2015 12=24 PM","USH","Complete","Home Drive Access Allowed"
Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly! Thank you so much.
lol sorry, was looking all over the place for a "Mark as answer" button, just realised the "tick" symbol under the down vote arrow was it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.