Powershell Script not working on Server 2022

Jeff Kartechner 0 Reputation points
2025-11-10T20:24:56.4533333+00:00

I have a powershell script that is working fine on Windows Server 2016 but throwing Error on Windows 2022.

Get-AzADGroup : Cannot bind argument to parameter 'DisplayName' because it is an empty string.
At E:\Scripts\PAIR_RC.ps1:186 char:52
...      $GroupObj = Get-AzADGroup -DisplayName $role.DisplayName # -Prop ...
                                            ~~~~~~~~~~~~~~~~~
CategoryInfo          : InvalidData: (:) [Get-AzADGroup], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Get-AzADGroup

Any thoughts?

Full Script is attached I could not any other forum for Powershell related questions.

Windows development | Windows API - Win32
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-10T20:25:16.3866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The error you are encountering, Cannot bind argument to parameter 'DisplayName' because it is an empty string, suggests that the $role.DisplayName variable is empty when the script is executed on Windows Server 2022. This could be due to several reasons, including changes in the environment or differences in how the script interacts with Azure AD on different server versions.

    Here are a few steps you can take to troubleshoot the issue:

    1. Check the Value of $role.DisplayName: Before the line that calls Get-AzADGroup, add a line to output the value of $role.DisplayName to ensure it is not empty:
         Write-Host "DisplayName: $($role.DisplayName)"
      
      This will help you confirm whether the variable is indeed empty when the script runs.
    2. Validate Input Data: Ensure that the data being passed to $role is correctly populated. If $role is being populated from a file or another source, verify that the source is accessible and contains the expected data.
    3. Check for Environment Differences: Since the script works on Windows Server 2016 but not on 2022, check for any differences in the installed modules or PowerShell versions. You can compare the output of Get-Module on both servers to see if there are discrepancies.
    4. Update Azure PowerShell Module: Ensure that you are using the latest version of the Azure PowerShell module on Windows Server 2022. You can update it using:
         Update-Module -Name Az
      
    5. Error Handling: Consider adding error handling around the Get-AzADGroup call to manage cases where the DisplayName might be empty, which can prevent the script from failing entirely:
         if (-not [string]::IsNullOrWhiteSpace($role.DisplayName)) {
             $GroupObj = Get-AzADGroup -DisplayName $role.DisplayName
         } else {
             Write-Host "DisplayName is empty for role: $role"
         }
      

    By following these steps, you should be able to identify the root cause of the issue and resolve it accordingly.

    0 comments No comments

  2. Tom Tran (WICLOUD CORPORATION) 3,120 Reputation points Microsoft External Staff Moderator
    2025-11-12T06:51:50.0633333+00:00

    Hi @Jeff Kartechner ,

    Thanks for sharing your script and the error details!

    From what you described, I think the main reason your script is failing on Windows Server 2022 is that it tries to look up Azure AD groups using their display name, but sometimes the display name is blank.

    When PowerShell sees an empty string for the -DisplayName parameter in the Get-AzADGroup command, it throws an error saying it can’t accept an empty string. This can happen if a group was renamed, deleted, or if there’s a data issue in Azure AD. It’s not a PowerShell version problem. Both your servers are running the same version but rather a difference in the data returned by Azure or the modules.


    I also think the problem happens in two places in your script, both times where you have a line like this:

    $GroupObj = Get-AzADGroup -DisplayName $role.DisplayName
    

    If $role.DisplayName is empty, the script crashes.

    To make your script more reliable, I’d recommend adding a quick check before calling Get-AzADGroup. If the display name is empty, you can look up the group by its object ID instead, which is always unique and present. Here’s how I would change those lines:

    $GroupObj = if ([string]::IsNullOrWhiteSpace($role.DisplayName)) {
        Get-AzADGroup -ObjectId $role.ObjectId
    } else {
        Get-AzADGroup -DisplayName $role.DisplayName
    }
    

    Just replace both instances of the original line with this new version.


    Using the group’s ObjectId is a reliable approach because every Azure AD group always has one, even if the display name is missing or blank. The Get-AzADGroup command fully supports looking up groups by ObjectId, as shown in Microsoft’s documentation for Get-AzADGroup.

    If you run into the same error elsewhere in your script (for example, if you look up groups by display name in other places), you can use the same pattern there too.


    Hope this helps! If you still have any questions, please feel free to comment below. I'll be happy to help out!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.