PowerShell: How manage users by OU

 

People hi,

 

Today we will manage users by Organization Unit.

The cmdlets to do that:

Get-ADUser -Filter * -SearchBase "OU=Vegetables,OU=Users,DC=BachToTheCloud,DC=COM"

Or with a filter on a Get-ADObject (you can also apply filter on groups/contacts/etc..)

Get-ADObject -Filter {ObjectClass -eq 'user'} -SearchBase "OU=Vegetables,OU=Users,DC=BachToTheCloud,DC=COM"

 

Example

I will get all users with the domain BachToTheCloud.com in the UPN field.

###Collect all users in the OU Vegetables
$Users = Get-ADObject -Filter {ObjectClass -eq 'user'} -SearchBase "OU=Vegetables,OU=Users,DC=BachToTheCloud,DC=COM"

### Process users one by one
foreach ($user in $Users) {
	#check if the UPN match with the domain
	if ( $user.UserPrincipalName -match "BachToTheCloud.com" ) {	
		Write-Host $User.Name
		Write-Host "$($User.UserPrincipalName)" -ForeGroundColor Green

	} else {
		$User.employeeID = 'Pomme'
		Write-Host $User.Name
		Write-Host "$($User.UserPrincipalName)" -ForeGroundColor Red

	}
}

More information or sources:

Get-ADUser https://technet.microsoft.com/en-au/library/ee617241.aspx

Get-ADObject https://technet.microsoft.com/en-us/library/ee617198.aspx

Leave a Reply