add "Remote Desktop User" powershell

PerserPolis-1732 1,971 Reputation points
2022-02-09T10:13:25.407+00:00

Hi,

how to add "Remote Desktop User" to the following script too?

New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'testtest') -Name $a -FullName "admin" -Description "Local Administrator" | Add-LocalGroupMember -Group administrators

regards

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.7K Reputation points MVP Volunteer Moderator
    2022-02-10T13:28:10.167+00:00

    Hi @PerserPolis-1732 ,

    you can try with adding the following line at the end:

    net user ($User.Name) /logonpasswordchg:yes  
    

    ----------

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

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.7K Reputation points MVP Volunteer Moderator
    2022-02-09T10:34:13.277+00:00

    Hi @PerserPolis-1732 ,

    maybe this works for you:

    $user = New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'testtest') -Name "Paul" -FullName "Paul" -Description "Local Administrator"  
    Add-LocalGroupMember -Group "Administrators" -Member $user  
    Add-LocalGroupMember -Group "Remote Desktop Users" -Member $user  
    

    ----------

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

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

  2. PerserPolis-1732 1,971 Reputation points
    2022-02-09T10:50:15.527+00:00

    No Sorry

    Add-LocalGroupMember : Cannot validate argument on parameter 'Member'. The argument is null or empty. Provide an
    argument that is not null or empty, and then try the command again.
    At G:\script\UserAccount.ps1:2 char:55

    • Add-LocalGroupMember -Group "Administrators" -Member $user
    • ~~~~~
    • CategoryInfo : InvalidData: (:) [Add-LocalGroupMember], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMemberComman
      d

  3. PerserPolis-1732 1,971 Reputation points
    2022-02-09T11:41:41.75+00:00

    Hi Andreas,

    the following script is working for me creating a local admin user. How can I add the "remote desktop User" too?

    $UserLogon = Get-WinEvent -LogName 'Microsoft-Windows-User Profile Service/Operational' `
    | Where-Object {$_.Id -eq 2} | Select-Object -First 1

    $LastUser = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$($UserLogon.UserId.Value)" -Name "ProfileImagePath"
    $substringIndex = $LastUser.ProfileImagePath.LastIndexOf("\") + 1
    $LastUserName = $LastUser.ProfileImagePath.Substring($substringIndex)
    $a=$LastUserName+"Admin"
    New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'password') -Name $a -FullName "Local Administrator" -Description "Local Administrator" | Add-LocalGroupMember -Group administrators

    Regards

    0 comments No comments

  4. Andreas Baumgarten 123.7K Reputation points MVP Volunteer Moderator
    2022-02-09T12:02:20.967+00:00

    Hi @PerserPolis-1732 ,

    please try this:

    $UserLogon = Get-WinEvent -LogName  'Microsoft-Windows-User Profile Service/Operational' `  
    | Where-Object {$_.Id -eq 2} | Select-Object -First 1  
         
    $LastUser = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$($UserLogon.UserId.Value)" -Name "ProfileImagePath"  
    $substringIndex = $LastUser.ProfileImagePath.LastIndexOf("\") + 1  
    $LastUserName = $LastUser.ProfileImagePath.Substring($substringIndex)  
    $a=$LastUserName+"Admin"  
    $user = New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'password') -Name $a -FullName "Local Administrator" -Description "Local Administrator"   
    Add-LocalGroupMember -Group administrators -Member $user  
    Add-LocalGroupMember -Group "Remote Desktop Users"  -Member $user  
    

    ----------

    (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

Your answer

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