Powershell help :(

Hi Team

Need help to filter users in a domain, here is the requirement......

I would like to get a list of users in a specific OU those are not member of a Specific Security group.

To be more clear we have several OU's( Country name ) and SubOU's (users..computers..external...etc)

we need all users list present in External OU... with above requirement

Simple right !!! not for me... please help.. 

August 14th, 2015 10:21am

Get-ADUser -filter * -Properties * -SearchBase "OU=MyOU,DC=mydomain,DC=local" | where {$_.memberof -notlike "*domain admins*"} | select name,userprincipalname

This example shows all users in "MyOU" organization unit of mydomain.com AD domain which are not member of domain admins AD group 

"OU=MyOU,DC=mydomain,DC=local" is distinguishedName attribute of OU.


Optionally you can set  -SearchScope option to limit your search to one OU without children. See more details https://technet.microsoft.com/en-us/library/Ee617241.aspx
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 11:05am

Get-ADUser -filter * -Properties * -SearchBase "OU=MyOU,DC=mydomain,DC=local" | where {$_.memberof -notlike "*domain admins*"} | select name,userprincipalname

This example shows all users in "MyOU" organization unit of mydomain.com AD domain which are not member of domain admins AD group 

"OU=MyOU,DC=mydomain,DC=local" is distinguishedName attribute of OU.


Optionally you can set  -SearchScope option to limit your search to one OU without children. See more details https://technet.microsoft.com/en-us/library/Ee617241.aspx
August 14th, 2015 3:03pm

Get-ADUser -filter * -Properties * -SearchBase "OU=MyOU,DC=mydomain,DC=local" | where {$_.memberof -notlike "*domain admins*"} | select name,userprincipalname

This example shows all users in "MyOU" organization unit of mydomain.com AD domain which are not member of domain admins AD group 

"OU=MyOU,DC=mydomain,DC=local" is distinguishedName attribute of OU.


Optionally you can set  -SearchScope option to limit your search to one OU without children. See more details https://technet.microsoft.com/en-us/library/Ee617241.aspx
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 3:03pm

Get-ADUser -filter * -Properties * -SearchBase "OU=MyOU,DC=mydomain,DC=local" | where {$_.memberof -notlike "*domain admins*"} | select name,userprincipalname

This example shows all users in "MyOU" organization unit of mydomain.com AD domain which are not member of domain admins AD group 

"OU=MyOU,DC=mydomain,DC=local" is distinguishedName attribute of OU.


Optionally you can set  -SearchScope option to limit your search to one OU without children. See more details https://technet.microsoft.com/en-us/library/Ee617241.aspx
August 14th, 2015 3:03pm

Get-ADUser -filter * -Properties * -SearchBase "OU=MyOU,DC=mydomain,DC=local" | where {$_.memberof -notlike "*domain admins*"} | select name,userprincipalname

This example shows all users in "MyOU" organization unit of mydomain.com AD domain which are not member of domain admins AD group 

"OU=MyOU,DC=mydomain,DC=local" is distinguishedName attribute of OU.


Optionally you can set  -SearchScope option to limit your search to one OU without children. See more details https://technet.microsoft.com/en-us/library/Ee617241.aspx
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 3:03pm

thank you for the help... 

But is not getting desired result.. the result is showing all user objects present in that particular OU.. second condition of security group is not getting fulfilled.

August 17th, 2015 5:30am

Here you are:

$Users = Get-ADUser -Filter * -Properties * | where {$_.Distinguishedname -like "*OU=External*"}
$Report = @()
foreach ($User in $Users) {
$USAM = $User.SamAccountName 
$UDN = $User.Distinguishedname 
$UPN = $User.UserPrincipalName 
$Memberof = Get-ADUser $USAM -properties memberof | select -expandproperty memberof | out-string  
$Out = New-Object PSObject; $Out | Add-Member -type NoteProperty UPN ($UPN) 
$Out | Add-Member -type NoteProperty Memberof ($Memberof); $Out | Add-Member -type NoteProperty Distinguishedname ($UDN) 
$Report += $Out 
}
$Report | where {$_.memberof -notlike "*YOUR GROUP NAME*"} | select UPN,Distinguishedname | export-csv C:\YOURFOLDER\File.csv

Free Windows Admin Tool Kit Click here and download it now
August 17th, 2015 9:53am

Hello,

Does it work?

August 18th, 2015 9:00am

no sir its not :(
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 12:06pm

Do not forget about asterisks before and after group name and OU. It works for me for any user in my specific *MyOU* and group like *domain admins*
August 21st, 2015 12:50pm

Do not forget about asterisks before and after group name and OU. It works for me for any user in my specific *MyOU* and group like *domain admins*

Hi Abhishek Saxena,

How is this going?

Regards,

Melon Chen
TechNet Community Su

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 5:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics