Manage Local Account users by PowerShell

30 בJanuary 2017

תגיות:
אין תגובות

Well, today I am going to share with you a good script which knows to remove any foreign user from a computer except certain user that you might specify.

In my script, I set to remove any local users except of ‘Administrator’ & ‘Visitor & ‘DefaultAccount’

$ComputerMember = [adsi]"WinNT://ComputerName" $Users = $ComputerMember.Children | where {$_.SchemaClassName -eq 'user'} $users.Name foreach ($user in $users.Name) { if($user -eq "Administrator" -or $user -eq "Visitor" -or $userv -eq "DefaultAccount") { } else { $ComputerMember.Delete("User",$user) } }

This script either can runs per OU or through TXT file, whenever you want.

How to Delete a specific Local Admin from all computer in AD and catch Error Message:

Second Script, which knows to delete a specific Local User from entire OU + output you TXT that indicates whether computers that weren’t available or computers that weren’t had the particular user

Please edit the following parameters:

> “SearchBase” the OU you want to search
>> Computer name ”
>”UserName-Change” supposed to hold the username you want to delete

$ComputerList = Get-ADComputer -Searchbase "DC=xx,DC=xx,DC=xx" -Filter {Enabled -eq $true -and name -like "xx3*" } 
$output = ""
Foreach ($machine in $ComputerList.Name)

{
    Try

    {
       
		$ComputerMember = [adsi]"WinNT://$machine"
	    $MESSAGE = $ComputerMember.Delete("User", "UserName-Change")
        $output = $output + $ComputerName +"Success`r`n"
    }

    Catch

    {
    $Errormessage = $_.Exception.Message
    $result = "($machine) ($ErrorMessage)"
    

   if ($ErrorMessage -like '*Exception calling "Delete"*')
    {
     $output = $output + $machine + " User Name NOT Found`r`n"

    }

    elseif($ErrorMessage -like "*Testing connection to computer*")
    {
     $output = $output+$machine+ "  Isn't Avalible`r`n"
    }
    else
    {
          $output = $output + $resultE
    }
    }
}
$output > c:\temp\Information.txt

I would like to thank Kobi Dikrman and Roy D.

Certainly, programmer might say that scripts below aren’t written correctly or aren’t wrote in the bet way that programmer would do, it is really important to clarify that I am not programmer and my purpose is just sharing IT solution which can reduce time spending, therefore I am definitely willing to get suggestion from you programmer how would you write it?

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *