Set-ADUser in multiple domains

Hi Guys,

I have a csv which contains UPNs for users which are within multiple domains in our forest. I need to clear the description for these accounts but for the life of me, I cannot get the script I have to work!

Current script

$Users = Import-csv C:\Temp\Users.csv
foreach($User in $Users){
Set-ADUser -server dc01.domain.co.uk:3268 $User.UserPrincipalName -Description $Null
}

Please format it as code using the code tags “PRE“. Thanks
You might read the instructions how to use this forum: Read Me Before Posting! You’ll be Glad You Did!

Try parsing the domain from the UPN assuming that the UPN is the domain name and not a mail domain:

#Emulate Import-csv C:\Temp\Users.csv
$Users = @"
jsmith@domainx.com
sjohnson@domainy.com
"@ | ConvertFrom-Csv -Header UserPrincipalName

foreach($User in $Users){
    $userDomain = ($user.UserPrincipalName -split '@')[1]

    'Connecting to {0} on domain {1}' -f $user.UserPrincipalName, $userDomain
    Set-ADUser -Identity $User.UserPrincipalName -Server $userDomain -Clear Description -WhatIf
}

Also, to clear a attribute value, you should use the -Clear parameter, not set the value to NULL manually.

Thanks, I’ve amended the post.

The domain name is a mix of the mail domain and the domain name.

The error I get is

“Set-ADUser : Cannot find an object with identity”.

I think the command cannot find the users in their domains.

 

We are at a bit of impasse as you are saying…

But providing no examples of a upn and what you are trying to extact. The basic idea is if you can extract that name from the UPN and it is resolvable in DNS, then you can use it as the -Server param and do the lookup in the proper domain. If DNS will not work, you would need to manually define DNS resolvable domain controllers to do the lookups.