Powershell – Remote Administer Local Groups with PowerShell and WMI

groupspowershellremote-accesswmi

How do I administer local groups on a remote server with PowerShell and WMI? For example, I would like to add and remove domain AD groups from the "Remote Desktop Users" group.

This article provides a script for listing users while this article provides a bit more detail on the Get-WMIObject (GWMI) and Set-WMIObject (SWMI) cmdlets, however I'm unsure how to proceed with updating the group membership.

Best Answer

ADSI is your friend

$server = "somesServer"
$domain = "someDomain
$group2add = "group2add"
$localGroup = [adsi]"WinNT://$server/administrators,group"
$domainGroup = [adsi]"WinNT://$domain/$group2add,group"
$localGroup.Add($domainGroup.path)