Custom Objects and Output to a csv file

Welcome Forums General PowerShell Q&A Custom Objects and Output to a csv file

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #128085

    Participant
    Points: 31
    Rank: Member

    Hi,

    I am using pieces of someone else's script I came across. The goal of the script is to search AD to see if an account exists. Long story short, my question is about output and objects. In the script, the output to screen for each item is done like this:

    if (!($ADResult)) {
    $HashProps = @{

    'HomeDir' = $_.FullName
    'AccountName' = 'None'

    }

    # Output the object to the screen
    New-Object -TypeName PSCustomObject -Property $HashProps

     

    However, I need to put that information into a csv file from within the script. I don't understand the custom objects, and though I have been researching this, I cannot figure out how to get the result I am looking for. The HomeDir and Accountnames should be headers in a csv file. I want the output to be done in the script.

     

    Would someone please help me with this? I would really appreciate it!

  • #128086

    Participant
    Points: 110
    Helping Hand
    Rank: Participant

    just need to pipe the new-object into the export-csv cmdlet (be sure to use the -append parameter to add a new line to the existing file)

    $HashProps = @{
    
    HomeDir = 'C:\SomePath'
    AccountName = 'OJSimpson'
    
    }
    
    # Output the object to the screen
     New-Object -TypeName PSCustomObject -Property $HashProps | Export-Csv -Path C:\Temp\temp.csv -NoTypeInformation -Append
    
  • #128157

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    @bvi1998. While the above reply answers your question, I would like to advice you to take some time to understand the basics in PowerShell. This will help you a lot.
    PowerShell in a Month of Lunches by Don Jones is the best book to start with.

  • #128239

    Participant
    Points: 31
    Rank: Member

    yes of course. I was in a pinch.

You must be logged in to reply to this topic.