No announcement yet.

using export-csv with for-each loop

  • Filter
  • Time
  • Show
Clear All
new posts

  • using export-csv with for-each loop

    I realise that this is probably a newbie question, but thats probably because I am just that - at least to Powershell.

    The code:
    add-PSSnapin  quest.activeroles.admanagement
    $AgeDays = 30
    $DomainControllers = @("domain1", "domain2", "domain3")
    for ($i=0; $i -lt $domaincontrollers.length; $i++) 
        connect-qadservice -service $DomainControllers[$i]
        Get-QADUser -CreatedAfter (Get-Date).AddDays(-$AgeDays) | select SamAccountName, WhenCreated | ft | out-string
    this gives me the information that I need (user objects created in the past 30 days) on the screen, but I need it in a csv file so I can email it to external consultants, can I use the export-csv cmdlet - I have tried and either get an empty file or a file that just contains type info
    Last edited by Rems; 9th June 2014, 18:42. Reason: added [code]-tags

  • #2
    Re: using export-csv with for-each loop

    To omit type info use the parameter -notypeinfo

    If there are new user objects then you can find them and export the results to csv file with the Get-QADUser line below,
    $AgeDays = 30
    $outFile = "c:\data\test.csv"
    Get-QADUser -CreatedAfter (Get-Date).AddDays(-$AgeDays) | select SamAccountName, WhenCreated | Export-CSV "$outFile" -notypeinfo -encoding "UTF8"
    The -Encoding parameter is optional ( I used it here just because I almost always use it.

    Last edited by Rems; 9th June 2014, 18:58.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts