Powershell script in Azure returns that property has no value

In my AzureAD/Entra ID tenant, I am trying to copy the data that is in the Department field into the CompanyName field. When I execute my script, the result is that are no values present in the Department field, but when I look at the users in the Azure GUI, there is data in there.

Any help would be appreciated. Here’s my script:

Get the list of users with JobTitle = “FALL24-Student”

$users = Get-AzADUser -Filter “jobTitle eq ‘FALL24-Student’”

foreach ($user in $users) {
# Retrieve the current Department value
$department = $user.Department

if ($department) {
    # Display the Department value
    Write-Output "User $($user.UserPrincipalName) has Department: $department"

    # Set the CompanyName to the value of Department
    Set-AzADUser -ObjectId $user.ObjectId -CompanyName $department

    # Confirm the update
    Write-Output "Updated user $($user.UserPrincipalName): CompanyName set to $department"
} else {
    Write-Output "User $($user.UserPrincipalName) does not have a Department value to copy."
}

}

I don’t see a “CompanyName” parameter on Set-AzAdUser
Set-AzAdUser
Try using -Company instead

CompanyName is the correct name for that property:

Hello, I’m going to convert this to MgGraph cmdlets. You need to use the -Property parameter to select specific properties to obtain.

connect-MgGraph -Scopes User.ReadBasic.All

$users = Get-MgUser -All -Filter "jobTitle eq 'FALL24-Student'" -Property Department, UserPrincipalName

foreach ($user in $users) {
    # Retrieve the current Department value
    $department = $user.Department
    if ($department) {
        # Display the Department value
        Write-Output "User $($user.UserPrincipalName) has Department: $department"

        # Set the CompanyName to the value of Department
        Update-MgUser -UserId $user.UserPrincipalName -CompanyName $department

        # Confirm the update
        Write-Output "Updated user $($user.UserPrincipalName): CompanyName set to $department"
    } else {
        Write-Output "User $($user.UserPrincipalName) does not have a Department value to copy."
    }
}
1 Like

you’re right. I imported the same module as you on my work computer and see that Set-AzADUser has a -CompanyName parameter that accepts string input.

Any chance it’s a permissions issue?

From a quick check, unlike Get-AzureADUser in the deprecated AzureAD module, Department is not in the default list of properties returned by Get-AzADUser.

You need to specify the Select parameter to return non-default properties and when specifying Select you need to specify all the properties you want.

You think Microsoft would alias Select as Property given we’re so used to that.

Get-AzADUser -Filter "jobTitle eq 'Retail Manager'" | 
     Select-Object UserPrincipalName, Department

UserPrincipalName             Department
-----------------             ----------
AdeleV@xxxxxx.onmicrosoft.com

Get-AzADUser -Filter "jobTitle eq 'Retail Manager'" -Select Department | 
    Select-Object UserPrincipalName, Department

UserPrincipalName Department
----------------- ----------
                  Retail

Get-AzADUser -Filter "jobTitle eq 'Retail Manager'" -Select UserPrincipalName, Department | 
   Select-Object UserPrincipalName, Department

UserPrincipalName             Department
-----------------             ----------
AdeleV@xxxxxx.onmicrosoft.com Retail
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.