locked
Get-ADUser from CSV, missing non-ADusers in output RRS feed

  • Question

  • Hi Guys,

    I have a CSV file with usernames (CN-field in AD). Column A starts with CN, then the usernames (or CN's of the users) below.

    From the file, i want to use Get-ADuser, see some properties and export this to a new CSV file. This works but one exception: In the input file there are also usernames that are not in AD. In the output file these users are filtered out, so they do not got exported to the new CSV file.

    I want these non existing users visible in my new CSV file with all the other users. A message like "User not found in AD" would be likeable.

    I am new to (Powershell) scripting, have read multiple forums and tried something like Try-Catch, or If. but i am failing everytime.

    Here is my code i'm using:

    import-csv H:\Test.csv | 
            ForEach-Object {
                Get-ADUser -Filter "CN -eq '$($_.CN)'" -Properties CN, DisplayName, Description
            } |
        select CN, DisplayName, Description | export-csv H:\Test_out.csv -NoTypeInformation
        H:\Test_out.csv

    Can anyone please help me with probably this simple issue? And any improvements on my current file is also appreciated.

    Thanks in advance

    Monday, April 15, 2019 9:46 AM

Answers

  • I am open for suggestions, so what did you had on your mind?

    -=Edit=-

    I changed it to below, which gave me the result I was looking for in the first place :-) So Thanks for that!

    Import-Csv -Path H:\Test.csv | 
    ForEach-Object {
        $User = Get-ADUser -Filter "CN -eq '$($_.CN)'" -Properties CN, DisplayName, Description -ErrorAction SilentlyContinue
        [PSCustomObject]@{
            SourceCN    = $_.CN
            CN          = If($User.CN){$User.CN}Else{'Account not found in AD'}
            DisplayName = $User.DisplayName
            Description = $User.Description
        }
    } | Export-Csv -Path H:\Test_out.csv -NoTypeInformation

    • Edited by Mikkelback Monday, April 15, 2019 1:25 PM
    • Marked as answer by Mikkelback Tuesday, April 16, 2019 6:37 AM
    Monday, April 15, 2019 1:19 PM

All replies

  • Import-Csv -Path H:\Test.csv | 
    ForEach-Object {
        $User = Get-ADUser -Filter "CN -eq '$($_.CN)'" -Properties CN, DisplayName, Description -ErrorAction SilentlyContinue
        [PSCustomObject]@{
            SourceCN    = $_.CN
            CN          = If($User.CN){$User.CN}Else{'n/a'}
            DisplayName = If ($User.DisplayName){}Else{'n/a'}
                Description = If($User.Description){}Else{'n/a'}
        }
    } | Export-Csv -Path H:\Test_out.csv -NoTypeInformation
    ... untested ...

    Live long and prosper!

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

    • Edited by BOfH-666 Monday, April 15, 2019 1:11 PM
    Monday, April 15, 2019 12:11 PM
  • Great, that works… Never thought of it writing it this way... So many thanks.

    One other question, to make it appealing easier to to see: Is it possible to write some text when $user.CN is an empty value? Something like "Account not found in AD"? I tried an IF-statement, but no result.

    if ($user.CN -eq $null) { write-verbose "Account not found in AD" }

    Monday, April 15, 2019 12:56 PM
  • Hmmm ... I wouldn't  like it this way ... but anyway, it's your decision ... I changed the code expample above.

    Live long and prosper!

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

    • Edited by BOfH-666 Monday, April 15, 2019 1:13 PM
    Monday, April 15, 2019 1:12 PM
  • I am open for suggestions, so what did you had on your mind?

    -=Edit=-

    I changed it to below, which gave me the result I was looking for in the first place :-) So Thanks for that!

    Import-Csv -Path H:\Test.csv | 
    ForEach-Object {
        $User = Get-ADUser -Filter "CN -eq '$($_.CN)'" -Properties CN, DisplayName, Description -ErrorAction SilentlyContinue
        [PSCustomObject]@{
            SourceCN    = $_.CN
            CN          = If($User.CN){$User.CN}Else{'Account not found in AD'}
            DisplayName = $User.DisplayName
            Description = $User.Description
        }
    } | Export-Csv -Path H:\Test_out.csv -NoTypeInformation

    • Edited by Mikkelback Monday, April 15, 2019 1:25 PM
    • Marked as answer by Mikkelback Tuesday, April 16, 2019 6:37 AM
    Monday, April 15, 2019 1:19 PM
  • That's a matter of taste. I'd prefer an empty cell instead of human readable message. But if you're already satisfied - great.  ;-) :-D

    Live long and prosper!

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

    Monday, April 15, 2019 1:28 PM