Need assistance with completing a script

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 week, 5 days ago.

  • Author
    Posts
  • #70469
    Profile photo of Powershell_Student
    Powershell_Student
    Participant

    Hi All,

    What I am trying to do is have a script that will look into ad for a specific OU that is specified. Output the OU name, username of the users in the OU and the display name of the user to a CSV file. I can get the output of the OU name and username but the display name isn't working. My thoughts on this is that some of the users are in a different domain we have a 1 way trust relationship. If its not possible to get the display names of the users from the other domain I will need a way to put some exception where it just leaves it blank for those users. This is what I have so far as my code.

    import-module ac*
    $groupName = Read-Host "Group name"
    
    Get-ADGroupMember -identity “$groupName” -recursive |select @{Expression={$groupName};Label="Group Name"}, @{Name="NetID";Expression={$_.SamAccountName}}  |Sort-Object "NetID"| Export-csv -path C:\outputfolder\Groupmembers-testagain.csv -NoTypeInformation -append
    
  • #70471
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Currently, your Select statement doesn't include the display name. What does your code look like when you try to include that?

  • #70483
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Get-ADGroupMember doesn't return the DisplayName property. You'll need to pipe into Get-ADUser to get more attributes of the user which should work across domain/forest trusts.

    Please test below and let us know:

    param (
      [Parameter(Mandatory = $true)]
      [String] $GroupName
    )
    
    Import-Module ActiveDirectory
    
    Get-ADGroupMember -Identity $GroupName -Recursive | 
      Get-ADUser -Properties DisplayName | 
        Select-Object -Property @{Name='Group Name'; Expression={ $GroupName }}, @{Name="NetID"; Expression={$_.SamAccountName}}, @{Name="Display Name"; Expression={$_.DisplayName}}  |
          Sort-Object "NetID" |
            Export-csv -Path C:\outputfolder\Groupmembers-testagain.csv -NoTypeInformation -Append
    
  • #70489
    Profile photo of Powershell_Student
    Powershell_Student
    Participant

    It worked. Thanks Daniel. I been working on this for months. Programming with powershell has never been my strong point. If there are any books or videos any of you suggest to get better at this let me know.

  • #70519
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Yes, I can recommend investing the time to watch the following video courses and buy/read/browse through Don's book "Learn Windows PowerShell in a Month of Lunches".

    Microsoft Virtual Academy: Jumpstart – Getting Started with Microsoft PowerShell
    https://mva.microsoft.com/en-US/training-courses/8276

    Microsoft Virtual Academy: Jumpstart – Using PowerShell for Active Directory
    https://mva.microsoft.com/en-US/training-courses/using-powershell-for-active-directory-8397

You must be logged in to reply to this topic.