Find CN with same string

Hi,

I am trying to find if a user has more than one account while trying to disable the user with the SAMAccountName.

Eg. IF the user has normal account like 20000 and Admin account like x20000, then disable both the accounts.

$User = Get-ADUser -Filter {CN -like '*20000'}

$UserName = $User.Name

It give me both the accounts but how do i disable both the accounts and not necessarily all accounts have two accounts.

How do I write a script to check in general and see if there is an account which start with x and then take an action for the x account.

  • It give me both the accounts but how do i disable both the accounts

    Your are already filtering for only what you want, so, just disable user object in your results, using the Disable-ADAccount cmdlet.

    How do I write a script to check in general and see if there is an account which start with x and then take an action for the x account.

    Just change your search string.

    $User = Get-ADUser -Filter {CN -like 'x20000*'}