Question

Each week I receive a list of computers and one username associated with each computer. I need to remove each particular user from the Administrators group on the associated computer.

Is there a way to script this?

Was it helpful?

Solution 2

I cannot fully test this, but you should be able to do essentially this:

Import the list:

Import-Csv "File.csv" | Foreach-Object {    
 $User= $_.Username    
 $PC = $_.ComputerName

 psexec "$PC" net user "$User" /delete 
}

This assumes you have header columns named Username and ComputerName.

OTHER TIPS

As I said in my comment on his answer Austin French's answer should work just fine, though it may be kind of slow since it relies on an expternal application (PSExec) to make the connection to the remote computers, perform the actions, and then report back to PowerShell. Also expect some nasty errors because PSExec doesn't exactly play nice with PowerShell.

As an alternative I would suggest using the ADSI Type Adapter [ADSI] to perform the actions through .Net functionality.

$List = Import-CSV File.CSV 
ForEach($Item in $List){
    $objUser = [ADSI]("WinNT://domain/$($Item.UserName)")
    $objGroup = [ADSI]("WinNT://$($Item.ComputerName)/Administrators")
    $objGroup.PSBase.Invoke("Remove",$objUser.PSBase.Path)
}

That should work just fine. Obviously you need to put your domain name in where it says domain, or if the user to be removed is a local user account you would change that to $($Item.computername) instead. This would, I am fairly certain, work much faster than PSExec and not rely on an external application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top