Solved

AD Query for Users - Add line for Domain Admins

Posted on 2013-02-01
3
365 Views
Last Modified: 2013-02-08
I posted a question on here recently that was answered already for running a AD powershell query for Users and exporting to a clewan CSV shoing Title name department etc...  I am wionder if it is possible to add to this cmd to also pull all Domain Admins in AD and list them in their own category on the CSV.  Below is the cmd SUBSUN provided foir running the AD USer Query and below that is a Query I found for pulling the DOmain Admins.  I am having trouble combining them to make one CSV.

Thanks in Advanced for your help!





COMMAND FOR AD USER QUERY:

Get-ADUser -Filter * -Properties Title,Department,LastLogonDate,whenCreated,Enabled -SearchBase "DC=ad,DC=local" |
? {$_.Name -notlike "*ConfRm*" `
 -and $_.SamAccountName -notlike "*IG*" `
  -and $_.SamAccountName -notlike "*Mail*" `
 -and $_.SamAccountName -notlike "*QPM*" `
     -and $_.DistinguishedName -notmatch 'OU=SharedMailboxes,DC=AD,DC=Local' `
   
      -and $_.DistinguishedName -notmatch 'OU=ServiceAccounts,DC=AD,DC=Local'}  |
Select Name,Title,Department,LastLogonDate,whenCreated,Enabled |
Export-Csv "C:\myscripts\ADusers.csv" –NoTypeInformation





CMD for Domain ADMIN Query:
Get-ADGroupMember 'domain admins'
0
Comment
Question by:Twhite0909
3 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38845776
One approach would be to use PSCustom objects to combine users from both queries
 
$usersCol = @()
# Get AD users
$users = Get-ADUser -Filter * -Properties Title,Department,LastLogonDate,whenCreated,Enabled -SearchBase "DC=ad,DC=local" |
? {$_.Name -notlike "*ConfRm*" `
 -and $_.SamAccountName -notlike "*IG*" `
  -and $_.SamAccountName -notlike "*Mail*" `
 -and $_.SamAccountName -notlike "*QPM*" `
     -and $_.DistinguishedName -notmatch 'OU=SharedMailboxes,DC=AD,DC=Local' `
         -and $_.DistinguishedName -notmatch 'OU=ServiceAccounts,DC=AD,DC=Local'}
# Get Domain Admins
$domainAdminUsers = Get-ADGroupMember -Identity "Domain Admins"

foreach ($user in $users)
{
	$objTemp = New-Object PSObject -Property @{            
		NAME = $user.Name
		TITLE = $user.Title
		DEPARTMENT = $user.Department
		LASTLOGONDATE = $user.LastLogonDate
		WHENCREATED = $user.WhenCreated
		ENABLED = $user.Enabled
		DOMAINADMIN = $false
	}            
	    
	$usersCol += $objTemp
}

foreach ($domainadmin in $domainAdminUsers)
{
	$adminuser = Get-ADUser -Identity $domainadmin.distinguishedName -Properties Title,Department,LastLogonDate,whenCreated,Enabled
			
	$objTemp = New-Object PSObject -Property @{            
		NAME = $adminuser.Name
		TITLE = $adminuser.Title
		DEPARTMENT = $adminuser.Department
		LASTLOGONDATE = $adminuser.LastLogonDate
		WHENCREATED = $adminuser.WhenCreated
		ENABLED = $adminuser.Enabled
		DOMAINADMIN = $true
	}

	$usersCol += $objTemp	
	
}

$usersCol | Export-Csv "C:\myscripts\ADusers.csv" -NoTypeInformation

Open in new window

0
 

Author Comment

by:Twhite0909
ID: 38851305
Ok so now I just need to pull the name sof the current domain admins tand thats all.  However when I run the following:

Get-ADGroupMember 'domain admins'


I get Name, Samaccount,name Distinguished name, SID, Object Class and Ibject GUID

How can I export to CSV but only pull the NAME and not the rest?

Thanks
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38855948
Try..
Get-ADGroupMember 'domain admins' | Select Name | Export-Csv C:\Admins.csv -NoTypeInformation

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now