Remove domain user from "Remote Desktop User" localgroupmember with Powershell

PerserPolis-1732 1,351 Reputation points
2022-02-09T15:35:23.177+00:00

Hi,

We have added on the local machine with Computer Management a domain user to local group of "Remote Desktop User" manually. Now I want to remove it per PowerShell on all machines.

Remove-LocalGroupMember -Group "Remote Desktop Users" -Member "username"

with above command line I have to put the "username" , is there any command line without putting the username?

-Name $username

Regards

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,381 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,926 Reputation points MVP
    2022-02-15T17:11:44.493+00:00

    Hi @PerserPolis-1732 ,

    to delete *David*from the Remote Desktop Users you can try this:

    $user2Remove = "David"  
    $localGroup = "Remote Desktop Users"  
    Get-LocalGroupMember -Group $localGroup | ForEach-Object {  
      if ($_.Name -match $user2Remove) {  
        Remove-LocalGroupMember -Group $localGroup -Member $_  
      }  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


6 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2022-02-09T16:12:16.083+00:00

    Put the user name into a file. Read the content of the file into a variable and use the variable in place of the quoted "username" literal.

    0 comments No comments

  2. PerserPolis-1732 1,351 Reputation points
    2022-02-10T07:41:10.677+00:00

    I don't know what do you mean. Could you please give me a example?

    0 comments No comments

  3. Andreas Baumgarten 96,926 Reputation points MVP
    2022-02-12T16:17:21.83+00:00

    Something like this:

    $usernames = "Peter", "Paul", "Marry"
    $localGroup = "Remote Desktop Users"
    $usernames | ForEach-Object {
      Remove-LocalGroupMember -Group $localGroup -Member $_
    }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. PerserPolis-1732 1,351 Reputation points
    2022-02-14T08:31:01.137+00:00

    Hi Andres,

    I will check it on my machine and let you know.

    Thank you for help.

    0 comments No comments