Home > Back-end >  how to change users company name of a certain department azure ad powershell
how to change users company name of a certain department azure ad powershell

Time:11-30

I have been assigned the task of finding users of a particular department and changing their Company Name in AzureAD via Powershell. I have come up with the below and tried using an If statement but keep getting a $Null return. Any ideas? My apologies in advance with how it looks I'm new to the coding world!

Connect-AzureAD -Credential $Credential

$department = 'Exiting'

$change = If (Get-AzureADUser -Filter "Department eq '$department'")

{ (Set-AzureADUser -ObjectId $change -CompanyName 'Exiting')
}

CodePudding user response:

Did Few Changed in your Script and it is working for me.

$department = 'Exiting'
$users = Get-AzureADUser -Filter "Department eq '$department'"
$users
foreach($user in $users){
(Set-AzureADUser -ObjectId $user.ObjectId -CompanyName 'Exiting')
}
Write-Output "Succesfull"
Write-Output "GET the Updated company name with Exiting"
Write-Output ""
Get-AzureADUser | ?{ $_.CompanyName -eq 'Exiting' }

enter image description here

Sample OutPut for above one of the user in Azure portal --

enter image description here

CodePudding user response:

Your code draft is unconstructed. First build a list of users to be changed. Then make a sequential change to those users using a foreach statement.

Connect-AzureAD -Credential $Credential
$department = "Exiting"
$newdepartment = "New Department Name"
$deptusers = (Get-AzureADUser -Filter "Department eq '$department'" | Select UserPrincipalName)
ForEach($deptuser in $deptusers) {
            
            #From Here What Ever Floats Your Boat :o) Either Line 10 or 12
            
            #You Can Get the User, then set the User
            #Get-AzureADUser -Filter "userPrincipalName -eq '$deptuser'" | Set-AzureADUser -CompanyName $newdepartment
            #You can just set the user department
            #Set-AzureADUser -ObjectId $deptuser -CompanyName $newdepartment
}
  • Related