locked
Powershell add string to AD export RRS feed

  • Question


  • Hi @all,

    I have a stupid beginner Problem. I do an AD export like this:

    $myDC = sever.domain.local

    Get-ADUser -Server $myDC -Filter * -Property whenCreated | Where {$_.whenCreated -ge '02/01/2020 00:00:00' -and $_.whenCreated -le '02/29/2020 23:59:59'} | Select Name,whenCreated | Export-Csv -NoTypeInformation -Append -Path c:\Export.csv




    Now I would like that each line of this csv file also contains the server name from $myDC. I did a lot of string manipulation before writing the csv file. But nothing ended up like I want.
    How can I split this output into an array or whatever so I can add the server information?

    Thanks Wolfgang


    Monday, March 9, 2020 2:17 PM

Answers

All replies

  • $myDC = sever.domain.local
    
    Get-ADUser -Server $myDC -Filter * -Properties whenCreated | 
        Where-Object { $_.whenCreated -ge '02/01/2020 00:00:00' -and $_.whenCreated -le '02/29/2020 23:59:59' } | 
            Select-Object -Property Name, whenCreated, @{Name = 'Server'; Expression = {$myDC}} | 
                Export-Csv -NoTypeInformation -Append -Path c:\Export.csv


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, March 9, 2020 2:28 PM
  • Many thanks :)

    You can't image how many complicated things I tested and now it's so easy! Great :)))

    Monday, March 9, 2020 2:53 PM
  • Here is the correct way to do this.  The above method return every user in aD which is much slower and also causes unneeded stress on a DC and on thenetwork.

    $myDC = 'sever.domain.local'
    $start = [datetime]'02/01/2020'
    $end = $start.AddMonths(1).AddDays(1)
    Get-ADUser -Server $myDC -Filter {WhenCreated -gt $start -and WhenCreated -lt $end} -Properties whenCreated | 
        Select-Object -Property Name, whenCreated, @{n = 'Server'; e = {$myDC}} | 
        Export-Csv -NoTypeInformation -Append -Path c:\Export.csv
    This is also the correct way to partition a date range with no holes.  This gets a complete month with no missing milliseconds.


    \_(ツ)_/


    • Edited by jrv Monday, March 9, 2020 8:51 PM
    • Proposed as answer by BOfH-666 Tuesday, March 10, 2020 1:53 AM
    Monday, March 9, 2020 8:51 PM
  • Just one more question. Is it also possible to format the output in the same short code, or do I have to do this in an extra step?

    I'd like to shrink in the target csv file the date to yyyyMM. Is there just a format syntax behind the 'whenCreated' possible or has the manipulation happen with the separate step before I write the csv file?
    Tuesday, March 10, 2020 9:58 AM
  • You should start learning basic Powershell. That would enable you to understand the help you get in technical forums like this.

    According to jrv's suggestion you can change the output by using calculated properties.

    $myDC = 'sever.domain.local'
    $start = [datetime]'02/01/2020'
    $end = $start.AddMonths(1).AddDays(1)
    Get-ADUser -Server $myDC -Filter {WhenCreated -gt $start -and WhenCreated -lt $end} -Properties whenCreated | 
        Select-Object -Property Name, @{Name = 'CreationMonth'; Expression = {$_.whencreated.ToString('yyyyMM')}}, @{Name = 'Server'; Expression = {$myDC}} | 
        Export-Csv -NoTypeInformation -Append -Path c:\Export.csv

    More stuff to read up about calculated properties:

    https://4sysops.com/archives/add-a-calculated-property-with-select-object-in-powershell/

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7

    https://www.pdq.com/blog/powershell-tips-for-system-administrators-calculated-properties/


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by TechNet2013 Tuesday, March 10, 2020 11:27 AM
    • Edited by BOfH-666 Tuesday, March 10, 2020 12:23 PM
    Tuesday, March 10, 2020 10:45 AM
  • Many thanks for your help. I'm learning from day to day and this lesson here helped me a lot of :) 

    Also many thanks for the links you sent me. The biggest problem is usually to have the rights word to find a solution. I'd never would have searched for 'calculated properties' to get this solved. 

    P.S.

    if you want to edit your post to help others too. You have forgotten to delete one 'whenCreted' string.


     

    Tuesday, March 10, 2020 11:27 AM
  • if you want to edit your post to help others too. You have forgotten to delete one 'whenCreted' string.

    Corrected. Thanks.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, March 10, 2020 12:24 PM