none
PS script to extract samAccountName, Name, Title and Department from username

    Question

  • I am very new to PS, so please bear with my ignorance.

    I am able to individually run these to get results but don't know how to stitch them together:
    Get-ADUser username -properties samAccountName, Name, Title, Department
    Get-ADGroupMember groupname | Select-Object name

    How do I go about creating a script that queries a AD group to find its members' samAccountName, Name, Title and Department?

    Once I get an extract, I need to change users' Title and Department. How do I update AD with the revised info?

    Friday, January 14, 2011 2:50 AM

Answers

  • Thanks! I used this and it worked:
    PS C:\> Get-ADGroupMember -Identity dls_contoso_staff -Recursive | Get-ADObject -Properties Name, Title, Department |Format-Table Name, Title, Department >c:\software\ur.csv

    This also works:
    PS C:\> Get-ADGroupMember GROUP_NAME -Recursive | select-object saMAccountName, Name| format-table -autosize

    • Marked as answer by ITAdminTO Friday, January 14, 2011 9:58 PM
    • Edited by ITAdminTO Tuesday, January 18, 2011 8:40 PM
    Friday, January 14, 2011 2:32 PM

All replies

  • For your first question you can pipe the one command into the other. For example:

    Get-ADGroupMember -Identity MyGroup | Get-ADUser -Properities sAMAccountName, Name, Title, Department

    You may want to use Format-Table to format the results. Also, errors will be raised if any of the members are not users. You can use Get-ADObject instead.

    For your second question, do you have a list of users to be modified, or all users, or all members of a group? And does everyone get the same Title and Department?

    Richard  Mueller


    MVP ADSI
    Friday, January 14, 2011 3:46 AM
  • Hi,

     

    Thanks for posting in Microsoft TechNet forums.

     

    As I understand that you want to update the user information in Active Directory. You could try the following scripts:

     

    ##Get the user names from the Group.

    $a=Get-ADGroupMember “Domain Users” –recursive | Select-Object name

    ##Get the properties of the users.

    $b=$a | Foreach-object {Get-ADUser $_.name –properties SamAccountName,Name,Title,Department}

     

    If you would like to change the title and department info. You could use Set-ADUser cmdlet to achieve this.

     

    ##Set the Title to test and Department to sales.

    $b | Foreach-object {Set-ADUser $_ -replace @{Title=”Test”;Department=”sales”}}

     

    For more information, please refer to:

     

    Set-ADUser

     

    Use PowerShell and Active Directory Cmdlets to Update Users in Active Directory

     

    Best Regards

    Dale Qiao
    TechNet Subscriber Support in forum. If you have any feedback on our support, please contact tngfb@microsoft.com


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Friday, January 14, 2011 7:45 AM
  • Thanks for the assistance. The script runs when I hit enter after typing $b.... and I can see all the usernames scroll up inside this error in red:

    Get-ADUser : Cannot find an object with identity: 'John Doe' under: 'DC=CONTOSO,DC=COM'. At line:1 char:35
    + $b=$a | ForEach-Object {Get-ADUser <<<<  $_.name -Properties samAccountName, Title}
        + CategoryInfo          : ObjectNotFound: (John Doe:ADUser) [Get-ADUser], ADIdentityNotFoundException
        + FullyQualifiedErrorId : Cannot find an object with identity: 'John Doe' under: 'DC=CONTOSO,DC=COM'
       '.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

     

    Friday, January 14, 2011 2:24 PM
  • Thanks! I used this and it worked:
    PS C:\> Get-ADGroupMember -Identity dls_contoso_staff -Recursive | Get-ADObject -Properties Name, Title, Department |Format-Table Name, Title, Department >c:\software\ur.csv

    This also works:
    PS C:\> Get-ADGroupMember GROUP_NAME -Recursive | select-object saMAccountName, Name| format-table -autosize

    • Marked as answer by ITAdminTO Friday, January 14, 2011 9:58 PM
    • Edited by ITAdminTO Tuesday, January 18, 2011 8:40 PM
    Friday, January 14, 2011 2:32 PM
  • For those who don't have Server 2008, try this:

    You will need Quest.Activeroles.ADManagement

    param( [string] $Group)
    Write-Host "Group Name: " $Group
    Add-PSSnapin Quest.Activeroles.ADManagement -EA silentlycontinue
    Get-QADGroupMember $Group |
    select DisplayName |
    Sort-Object $_.DisplayName |
    %{Get-QADUser -DisplayName $_.DisplayName} |
    select  Name, Title, Department |
    %{$_.Name + "," + $_.Titlel + "," + $_.Department}|
    Out-File "c:\temp\ur.csv" -Encoding ASCII

    Friday, January 14, 2011 6:31 PM
  • Anyway, I'm glad to see that you got the script to work.

    Best Regards

    Dale Qiao
    TechNet Subscriber Support in forum. If you have any feedback on our support, please contact tngfb@microsoft.com


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Monday, January 17, 2011 1:20 AM