Hi,
Here is the script you are looking for :
$ADUser = [ADSI]"LDAP://CN=UserName,OU=Users,DC=TestDomain,DC=com"
$ADUser.SamAccountName #to check the account
$ADUser.psbase.invokeSet("allowLogon",0)
$ADUser.setinfo()
And if you wanna have more information about this, maybe you can reference these sites:
How to Remotely Enable and Disable (RDP) Remote Desktop?
By default on a Windows Server Product Windows Remote Management (WinRM) is enabled, but Remote Desktop (RDP) is Disabled. On workstation operating systems neither is enabled by default, so if you want to be able to accomplish the following you will need to enable WinRM on the workstations.
https://www.interfacett.com/blogs/how-to-remotely-enable-and-disable-rdp-remote-desktop/
How to enable Remote Desktop using Powershell
If you are using Windows 2012 R2 Core or if you just like using Powershell, then you may want to know how to enable Remote Desktop.
To do this, you should go into your Core server where you should see a command box. In here, type in Powershell and press enter. This will open up the Powershell console for you to use.
https://blog.techygeekshome.info/2014/07/how-to-enable-remote-desktop-using-powershell/
PowerShell Problem Solver: Active Directory Remote Desktop Settings?
During my recent PowerShell workshop in Finland, an attendee asked about Active Directory cmdlets from Microsoft in regards to remote desktop user settings. Although you can readily see the settings in Active Directory Users and Computers, Get-ADUser doesn’t retrieve them. I haven’t worked with Remote Desktop Services in quite a while, but I told him I’d look into this long-standing problem.
https://www.petri.com/powershell-problem-solver-active-directory-remote-desktop-settings
Really hope these can help you.
Best wishes,
Young Yang.