Exchange Management Shell Filter users based on an existing attribute

Chris Martinez 21 Reputation points
2022-10-03T21:00:33.82+00:00

In Exchange Management Shell I run these commands to try to set a couple of users remote powershell access to disabled:

$DSA = Get-User -ResultSize Unlimited -Filter "(RecipientType -eq 'UserMailbox') -and (Title -like 'Sales Associate')"

$DSA | foreach {Set-User -Identity $_ -RemotePowerShellEnabled $false}

After I input the 2nd command, I get this response from the shell:
cmdlet Set-User at command pipeline position 1
Supply values for the following parameters:
Identity:

I am not sure what I am supposed to do. I got the instructions from control-remote-powershell-access-to-exchange-servers and it does not mention this part.

Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,327 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,346 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,621 Reputation points
    2022-10-03T21:26:02.037+00:00

    You can't use the user object as the identity. You can, however, use a property of the user object:

    $DSA | foreach {Set-User -Identity $_.distinguishedname -RemotePowerShellEnabled $false}  
    

    Because you already have an array of user objects in the $DSA variable, using a pipeline is less efficient than using a simple "foreach" statement:

    foreach ($u in $DSA){Set-User -Identity $u.distinguishedname -RemotePowerShellEnabled $false}  
    

    If you prefer to use a pipeline (and avoid the overhead of creating an intermediate array just to one time), then this is better:

    Get-User -ResultSize Unlimited -Filter "(RecipientType -eq 'UserMailbox') -and (Title -like 'Sales Associate')" |  
        foreach-object {Set-User -Identity $_.distinguishedname -RemotePowerShellEnabled $false}  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KyleXu-MSFT 26,206 Reputation points
    2022-10-04T02:05:49.563+00:00

    @Chris Martinez

    Run command blow first, make sure it could find the correct users on your Exchange server:

    $DSA = Get-User -ResultSize Unlimited -Filter "(RecipientType -eq 'UserMailbox') -and (Title -like '*Sales Associate*')"  
    $DSA   
    

    If there doesn't exist output, it means the filer that you used isn't correct. You need to modify the filter first.

    Here is an example in my lab:
    247217-1.png

    Please note, there exist * in the command. You also need to pay attention to the Space between the $_ and -RemotePowerShellEnabled.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.