active directory department issue


as in many organizations we have people with an ADM account.
I would also like to include these in our offboarding process and I’m running into problems with the following:
[pre class=“decode:true”]
$user =“paul.adm”
if(!($user -contains “.adm”)){

$department = (get-aduser $user -properties department).department
$user = $tempuser.split(".")

$department = (get-aduser $tempuser[0] -Properties department).department

if I run the above mentioned code my department remains empty
If I run the else part of my if statement then this works correctly and gives me the department name.
what is the reason that it doesn’t work in my if statement?

thanks for your help


-contains is for checking if a collection contains a object, it’s not meant for string comparisons. Try changing your statement to

if ($user -notlike ‘*.adm’) {}

Looks like your variables are mixed up in the else statement too.

Assuming the disable process is the same for both accounts (which you could do certain steps for non-adm with a simple if), why not try something like this:

$user = 'paul'
$userAdm = '{0}.adm'

$users = Get-ADUser -Filter {(SamAccountName -eq $user) -or (SamAccountName -eq $userAdm)} -Properties Description, Department

foreach ( $u in $users ) {
    'Processing {0}' -f $u
    Set-AdUser -Identity $u -Description ('Disabled {0} - {1}' -f  (Get-Date -Format "d"), $u.Description)