Share via


remove local user remotely for list of machines

Question

Saturday, November 24, 2018 9:49 AM

Hi,

i want remove specific local user from list of machines remotely i need your help.

Mohamed Soliman System Administrator +971552997724

i can do that for single machine using this script, is it possible to apply for list of machines

Function Delete-User {
     #Deletes a local user account on target computer
     Param     (
          [string]$computer,
          [string]$user
    )
     
     [ADSI]$server = "WinNT://$computer"
     $server.delete("user",$user)
}
Delete-User -computer 'machineName' -user 'UserName'

All replies (8)

Saturday, November 24, 2018 3:36 PM ✅Answered

Create a file named Accounts.csv that has contents like this:

Server,User
server1,Bill
server2,Bob

Add this line at the end of your ps1 file. 

Import-Csv -path C:\temp\Accounts.csv | %{ Delete-User -computer $_.server -user $_.user   }


Monday, November 26, 2018 3:09 AM ✅Answered

Hi,

I can't find the  "Delete-User" cmdlet in the microsoft document. I only found "remove-localuser" cmdlet.

Import-Csv -path C:\temp\Accounts.csv | %{ Invoke-Command -ComputerName $_.server -ScriptBlock {Remove-LocalUser -Name $_.name} }

/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localuser?view=powershell-5.1

Best Regards,

Lee

Just do it.


Monday, November 26, 2018 2:34 PM ✅Answered | 1 vote

Lee, 

Delete-user is a function that Mohamed included in his initial post. I assume that he found it somewhere on the net in a script repository.  It is not part of Powershell. It is a user defined function.  

-Dave

Function Delete-User {
     #Deletes a local user account on target computer
     Param     (
          [string]$computer,
          [string]$user
    )
     
     [ADSI]$server = "WinNT://$computer"
     $server.delete("user",$user)
}
Import-Csv -path C:\temp\Accounts.csv | %{ Delete-User -computer $_.server -user $_.user   }

Monday, November 26, 2018 8:17 PM

Thanks Moto for your quick response, it worked realy appreciated.

Mohamed Soliman System Administrator +971552997724


Tuesday, November 27, 2018 1:19 AM

Sorry. I got it.

Just do it.


Thursday, November 29, 2018 11:10 AM

Thanks boss its working, is it possible to disable local user for list of machines using ADSI command.

Mohamed Soliman System Administrator +971552997724


Thursday, November 29, 2018 11:12 AM

Thanks boss, is it possible to disable local user for list of machines using ADSI command.

Mohamed Soliman System Administrator +971552997724


Thursday, November 29, 2018 12:23 PM

Hi,

Thanks for your question.

Please refer the link below:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/30e90218-57a5-459b-9555-c687aa6f52ba/how-to-disable-local-user-account-on-over-200-servers-using-script?forum=ITCG

Best Regards,

Lee

Just do it.