Remove owner from ldap based on uid for particular group

$MTauthenticationType = [System.DirectoryServices.AuthenticationTypes]::ServerBind
$MTobjSearcherRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://,o=********", "uid=*******,ou=mtworkers,o=********", "********" , $MTauthenticationType)
$MTobjSearcher = New-Object System.DirectoryServices.DirectorySearcher($MTobjSearcherRoot)
$MTobjSearcher.ClientTimeout = New-Object System.TimeSpan(0, 0, 61, 0, 0);
$MTobjSearcher.Filter = "cn=KTGROUP4"
$MTresult = $MTobjSearcher.FindAll().Properties

$RemovedUser= $MTresult.Remove('NSIROHI')

// list of the owner looking like 

PS C:\WINDOWS\system32> C:\Users\nsirohi\Desktop\Untitled1.ps1

Name                           Value                                                                                                                                              
----                           -----                                                                                                                                              
mailaliased                    {0}                                                                                                                                                
contactusername                {uid=NSIROHI, ou=mtworkers,}                                                                                                          
lastreviewdate                 {20211029}                                                                                                                                         
adspath                        {LDAP://,ou=mtgroups,o=*****}                                                                                 
objectclass                    {top, groupofuniquenames, micron}                                                                                                                  
lastreviewby                   {1308732}                                                                                                                                          
cn                             {KTGROUP4}                                                                                                                                         
companynumber                  {1000}                                                                                                                                             
reviewdate                     {20221029}                                                                                                                                         
owner                          {uid=NSIROHI, ou=mtworkers,, uid=NIKHILJAIN, ou=mtworkers, o=***** uid=ROOHINAS, ou=mtworkers, o=********}

When posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

How to format code on

Are you having a problem with your code? Please let us know how we can help.

1 Like
Hi  All,

I tried with someway

 $b =$a.owner| ?{$_  -like 'uid=NSIROHI*'}

In $b I'm getting  output 

uid=NSIROHI, ou=mtworkers,

Now I want to remove UID= NSIROHI from here.


It's throwing an error like 
 Cannot find an overload for "Remove" and the argument count: "0".
At line:1 char:1
+ $b.Remove()
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest