locked
remove local user remotely for list of machines RRS feed

  • Question

  • 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'

    Saturday, November 24, 2018 9:49 AM

Answers

  • 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 2:34 PM
  • 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   }


    Saturday, November 24, 2018 3:36 PM
  • 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} }

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localuser?view=powershell-5.1

    Best Regards,

    Lee


    Just do it.

    Monday, November 26, 2018 3:09 AM

All replies

  • 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   }


    Saturday, November 24, 2018 3:36 PM
  • 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} }

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localuser?view=powershell-5.1

    Best Regards,

    Lee


    Just do it.

    Monday, November 26, 2018 3:09 AM
  • 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 2:34 PM
  • Thanks Moto for your quick response, it worked realy appreciated.

    Mohamed Soliman System Administrator +971552997724

    Monday, November 26, 2018 8:17 PM
  • Sorry. I got it.

    Just do it.

    Tuesday, November 27, 2018 1:19 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:10 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 11:12 AM