Need assistance with completing a script

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

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

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

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.

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