locked
Distinguish User from Group RRS feed

  • Question

  • Dear All

    I have an excel sheet with 2000 entries and i need to distinguish who is a user and who is a group.  So far i only came acrossthe below commands suggested from a friend. 

    I have imported my CSV which is called users and has a header user as well.

    If i type in powershell @users i am getting all the users in my CSV but when i run the below nothing happens.

    Can you please assist me?

    $users=impot-csv c:\users.csv
    ForEach($user in $users){
    Get-ADObject -Filter {Name -eq "$user"}|select Objectclass
    }

    Tuesday, December 18, 2018 9:57 AM

Answers

  • Ah, if the csv contains sAMAccountNames (which is better anyway), then modify your filter to filter on sAMAccountName instead of Name. Since your heading in the file is "User", use:

    $users=Import-csv c:\users.csv
    ForEach($user in $users)
    {
        $NTName = $User.User
        Get-ADObject -Filter {sAMAccountName -eq $NTName} | select Objectclass
    }

    The Name is the Relative Distinguished Name of the object (value of the cn attribute of users and groups). It need only be unique in the parent OU or container. There can be duplicates. The sAMAccountName (pre-Windows 2000 logon name in ADUC) is unique among all objects in the domain. Contact objects are not security principals and so do not have a sAMAccountName.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Tuesday, December 18, 2018 2:23 PM

All replies

  • Change impot-csv to import-csv.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, December 18, 2018 10:39 AM
  • Dear Richard

    Apologies that was just a typo here i know the command:) When running the command i don't get any results on the screen. pasting below the correct command

    I have a csv with header user to import.

    $users=Import-csv c:\users.csv
    ForEach($user in $users){
    Get-ADObject -Filter {Name -eq "$user"}|select Objectclass
    }

    Tuesday, December 18, 2018 12:55 PM
  • If you're CSV contains the usernames then you're using the wrong command. Try this:

    $users=Import-csv c:\users.csv
    ForEach($user in $users){
    Get-ADUser -Identity $user | select Objectclass
    }

    If not, which value is within the CSV?

    Tuesday, December 18, 2018 1:12 PM
  • The coomand Get-Aduser will only diplsay user info. my csv contains groups as well but i need to distinguish which one is which somehow. is there any way to make powershell to display the information on all the objects in my csv ?
    Tuesday, December 18, 2018 1:24 PM
  • Is user the actual users name 'John Doe' or is it the samaccount name jdoe?

    Import-Csv C:\temp\users.csv | Foreach {
      Get-AdObject -Filter "SamAccountName -eq '$($_.User)'" | Select ObjectClass 
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Tuesday, December 18, 2018 3:35 PM updated script
    Tuesday, December 18, 2018 1:30 PM
  • Then you're likely passing the wrong information, ensure the name you're using matches the name Get-ADObject expects.
    Tuesday, December 18, 2018 1:47 PM
  • All i have is samaccounts. some users some groups. i can go one by one but we are talking approximately 2500 entries:)
    Tuesday, December 18, 2018 1:47 PM
  • Is user the actual users name 'John Doe' or is it the samaccount name jdoe?

    Import-Csv C:\temp\users.csv | Foreach {
      Get-AdObject -Filter "SamAccountName -eq '$_'" | Select ObjectClass 
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    If the "CSV" contain only samaccountnames without header, the first element would be the header of the CSV
    import-csv .\users.txt -header name |% {
     get-adobject -filter "samaccountname -eq '$($_.name)'" |
     select name,objectclass
    }
    Also, result containing only ObjectClass property without Name reference is a bit useless, don't u think? :)
    Tuesday, December 18, 2018 2:21 PM
  • Ah, if the csv contains sAMAccountNames (which is better anyway), then modify your filter to filter on sAMAccountName instead of Name. Since your heading in the file is "User", use:

    $users=Import-csv c:\users.csv
    ForEach($user in $users)
    {
        $NTName = $User.User
        Get-ADObject -Filter {sAMAccountName -eq $NTName} | select Objectclass
    }

    The Name is the Relative Distinguished Name of the object (value of the cn attribute of users and groups). It need only be unique in the parent OU or container. There can be duplicates. The sAMAccountName (pre-Windows 2000 logon name in ADUC) is unique among all objects in the domain. Contact objects are not security principals and so do not have a sAMAccountName.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Tuesday, December 18, 2018 2:23 PM
  • ok then i will go with the commands you pasted and come back with an update. thank you so much
    Tuesday, December 18, 2018 2:29 PM
  • get-content .\users.txt |%{
     get-adobject -filter "samaccountname -eq '$_'" | 
     select name,objectclass
    }

    If u have "clean" txt file containing samaccountnames - without white spaces  .. this would work too  (less confusing than having CSV which is not actually CSV at all)
    Tuesday, December 18, 2018 2:30 PM
  • Is user the actual users name 'John Doe' or is it the samaccount name jdoe?

    Import-Csv C:\temp\users.csv | Foreach {
      Get-AdObject -Filter "SamAccountName -eq '$_'" | Select ObjectClass 
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    If the "CSV" contain only samaccountnames without header, the first element would be the header of the CSV
    import-csv .\users.txt -header name |% {
     get-adobject -filter "samaccountname -eq '$($_.name)'" |
     select name,objectclass
    }
    Also, result containing only ObjectClass property without Name reference is a bit useless, don't u think? :)
    DOH!!! Thanks for catching that :-)

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Tuesday, December 18, 2018 2:42 PM
  • Sorry, I realized that using $User wouldn't work in your script. Since you said the csv had the heading "User", I modified my answer above.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, December 18, 2018 2:43 PM
  • Dear Richard 

    I can't thank you enough seriously. Your version worked. I added to Select-Object Objectclass, name as well and also it assigned next to it the user or group. 

    One more think please and thank you again. When adding the command Export-csv C:\results.csv it doesn't write all the results in a csv only one line. Any ideas? Same goes if i try to write it on a txt file.

    Wednesday, December 19, 2018 8:17 AM
  • Dear Richard

    Never mind i copied from the console.

    Thank you so much for this help as i have this as deadline. This version worked flawless and again i can't thank you enough:)

    Wednesday, December 19, 2018 8:21 AM
  • You are welcome

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Wednesday, December 19, 2018 10:42 AM