Foreach issue with .csv export

Welcome Forums General PowerShell Q&A Foreach issue with .csv export

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

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #174532

    Participant
    Topics: 7
    Replies: 9
    Points: 70
    Rank: Member

    Hi All,

    When i use the script  below the script run with out any failure.

    But when i open my .csv file i only get the latest user in de file.... how can i ensure that all users will be in the .csv file?

    Thanks already!

    foreach ($user in $users)
    {
    $test = Get-ADUser -server server1 -Identity “$user” -Credential $Cred -Properties * | select samaccountname, userprincipalname, lastlogondate
    $test | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation
    }
  • #174535

    Participant
    Topics: 4
    Replies: 54
    Points: 382
    Helping Hand
    Rank: Contributor

    Hi All,

    When i use the script below the script run with out any failure.

    But when i open my .csv file i only get the latest user in de file.... how can i ensure that all users will be in the .csv file?

    Thanks already!

    PowerShell
    5 lines

    1
    2
    3
    4
    5
    foreach ($user in $users)
    {
    $test = Get-ADUser server server1 Identity$userCredential $Cred Properties * | select samaccountname, userprincipalname, lastlogondate
    $test | Export-Csv Path C:\temp\allusers2.csv NoTypeInformation
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Use the -append option

     

    
    Export-Csv -Append
    
    
  • #174541

    Participant
    Topics: 7
    Replies: 9
    Points: 70
    Rank: Member

    Hi Bart,

    Thanks for you reply, but i doesnot work.

    I think my failure is already earlier in the script.

    When i just do

    foreach ($user in $users)
    {
    $test = Get-ADUser -server server1 -Identity “$user” -Credential $Cred -Properties * | select samaccountname, userprincipalname, lastlogondate}

    Also my $test has just got one user in it, instead of many user.... what do i do wrong?

  • #174547

    Participant
    Topics: 4
    Replies: 54
    Points: 382
    Helping Hand
    Rank: Contributor

    Ok little example

     

    $t = 1..10
    $d = @()
    
    foreach ($item in $t)
    {
    $item
    $d += $item
    }
    
    $d | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation
    
  • #174559

    Participant
    Topics: 20
    Replies: 32
    Points: 160
    Helping Hand
    Rank: Participant

    Hi,

    Use convertto-csv to export the output.

    Ex.

    
    $test|convertto-csv | out-file .\test.csv -append
    
    

    There is also another way, but I hope it will work. Plz check and let us know.

  • #174604

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Paul1984,

    Here you go.

    $users = 'Jason.Robertson','Jason.Test'
    
    $Test = New-Object -TypeName System.Collections.Hashtable
    
    foreach ($user in $users)
    
    {
    
    $test+=Get-ADUser-Identity $user-Properties LastLogonDate |Select-Object samaccountname, userprincipalname, lastlogondate
    
    }
    
    $test | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation

     

  • #174631

    Participant
    Topics: 10
    Replies: 59
    Points: 143
    Helping Hand
    Rank: Participant

    What I normally do is similar to Jason.Robertson but instead of concatenating each object into a variable inside a loop, I assign the foreach output to a variable and pipe it to Export-Csv:

    $userCollection = foreach ($user in $users)
    {
        Get-ADUser -server server1 -Identity $user -Credential $Cred -Properties LastLogonDate | select samaccountname, userprincipalname, lastlogondate
    }
    
    $userCollection | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation

     

  • #174667

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    @Aaron,

    Its great learning new ways to collect and store the data. I use this for my condition statements, don't know why it never occurred to me do to that for the Foreach loop.

     

  • #175696

    Participant
    Topics: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: Contributor

    how about....

    $users | %{Get-ADUser -server server1 -Identity $_ -Credential $Cred -Properties LastLogonDate | select samaccountname, userprincipalname, lastlogondate} | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation -append
  • #175747

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Hello Ian,

    This would work just fine if you are needing to run a one line, however if you writing a script the proper structure would be to use a foreach loop instead. I personally would not add the -append to the Export-Csv unless explicitly requested by the requestor as we don't know the expectation of the export. It could very well be that Paul expects the data to be overwritten each time. 🙂

You must be logged in to reply to this topic.