Exporting members of "Domain Admins" group with specific attributes.

Hi Team,

I'm trying to extract Domain Admins members all all below details, but the lastLogonTimestamp, useraccountcontrol, whenChanged,whenCreated is returning blank values.

Get-ADGroupMember -Identity "Domain Admins" |Select-Object -Property Name,@{name="Description";expression={(Get-ADUser -Identity $_.SamAccountName -Properties Description).Description}},SamAccountName, lastLogonTimestamp,useraccountcontrol,whenChanged,whenCreated

Open in new window


Something small, but can't figure it. Need help.

Thanks.
Nitin PandeyInfrastructure EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Because Get-ADGroupMember only returns objects with only the very basic properties. Everything else, you have to get yourself (basically like you started to do with the description):
Get-ADGroupMember -Identity "Domain Admins" |
	Where-Object {$_.objectClass -eq 'user'} |
	Get-ADUser -Property Description, lastLogonTimestamp, userAccountControl, whenChanged, whenCreated |
	Select-Object -Property Name, Description, SamAccountName, lastLogonTimestamp, Useraccountcontrol, WhenChanged, WhenCreated

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Senior IT System EngineerSenior Systems EngineerCommented:
Try this one:

$filePath = "C:\Logs\"
$ADGroups = Get-ADGroup -Identity 'Domain Admins' -Filter * -Properties * | Where-Object GroupCategory -eq "Security" | Sort-Object Name 
foreach ($group in $ADgroups) {
    Get-ADGroupMember $group.Name |
        Select-Object -Property Name,
        @{n = 'Group Name'; e = {$group.Name}},
        @{n = 'User Name'; e = {$_.Name}},
        @{name = "Description"; expression = {(Get-ADUser -Identity $_.SamAccountName -Properties Description).Description}}, 
        SamAccountName, 
        lastLogonTimestamp, 
        useraccountcontrol, 
        whenChanged, 
        whenCreated |
        Export-Csv "$($filePath)\SecurityGroupMembers.csv" -NoTypeInformation -Append
}

Open in new window

Nitin PandeyInfrastructure EngineerAuthor Commented:
Thanks a ton guys. The lastLogonTimestamp was showing up with characters, but I converted it in Excel using =IF(D2>0,D2/(8.64*10^11) - 109205,"") in a new column E.

Can this be handled in the powershell output directly?
oBdACommented:
Sure:
Get-ADGroupMember -Identity "Domain Admins" |
	Where-Object {$_.objectClass -eq 'user'} |
	Get-ADUser -Property Description, lastLogonTimestamp, userAccountControl, whenChanged, whenCreated |
	Select-Object -Property Name, Description, SamAccountName, @{n='LastLogonTimeStamp'; e={[DateTime]::FromFileTime($_.lastLogonTimeStamp)}}, Useraccountcontrol, WhenChanged, WhenCreated 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.