Set -RemotePowerShellEnabled on Multiple Accounts with Duplicate Display Names

Championface 20 Reputation points
2024-07-17T17:14:40.9666667+00:00

Hey team,

I've been racking my brain for the last hour and I think I just need a fresh perspective.

In short -

The following command would disable remote PowerShell for all users within the tenant:

Get-User | Set-User -RemotePowerShellEnabled $false

Given the size of the tenant, there are multiple accounts with the same display name so the command fails about 1/4 of the way through. The following command shows me the email address/username (rather than the display name) of each account, and the remote PowerShell enabled status:

Get-User -ResultSize Unlimited | Select-Object UserPrincipalName, RemotePowerShellEnabled

How can I append (or pipe) the first command to change the RemotePowerShellEnabled to "false" using the UserPrincipalName rather than the display name? I'm trying to avoid creating individual Set-User commands for thousands of accounts, if possible.

Thanks in advance

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,368 questions
Microsoft Exchange Online
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,332 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vasil Michev 100.2K Reputation points MVP
    2024-07-17T18:27:04.4266667+00:00

    Something like this should do:

    Get-User -Filter {RemotePowerShellEnabled -eq $true} | % { Set-User -Identity $_.UserPrincipalName -RemotePowerShellEnabled $false -Confirm:$false }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful