Foreach issue with .csv export

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
}

[quote quote=174532]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
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
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
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] Use the -append option

 

[pre]

Export-Csv -Append

[/pre]

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?

Ok little example

 

[pre]
$t = 1…10
$d = @()

foreach ($item in $t)
{
$item
$d += $item
}

$d | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation
[/pre]

Hi,

Use convertto-csv to export the output.

Ex.

[pre]

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

[/pre]

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

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

 

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

 

@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.

 

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

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. :slight_smile: