Solved

AD Query for Users - Add line for Domain Admins

Posted on 2013-02-01
3
361 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This article will help you understand what HashTables are and how to use them in PowerShell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

20 Experts available now in Live!

Get 1:1 Help Now