Multiple local account on Multiple servers erase

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
chill3r
Posts: 4
Joined: Tue Nov 07, 2017 6:18 am

Multiple local account on Multiple servers erase

Post by chill3r » Tue Nov 07, 2017 6:25 am

Hi, i have been a lurker for sometime trying to get my head round PS.

i have an issue at the moment - i need to Script or Try to script a way to remove Local Accounts from Multiple machines, we have a list of about 300 Servers and a list of accounts.

The only way we can connect is via PS Remoting, so i need to know som pointers on this :

i have the below :

Code: Select all

$Computers = Get-Content C:\script\Computers.txt
ForEach ($Computer in $Computers) {
    $Session = New-PSSession -ComputerName $Computer

Invoke-Command -Session $session -ScriptBlock {}
But what would be the best command to remotely remove a local account Or check to see if the account exists and if it does remove it then move onto next server.

I also need to add some output to this to see if the account has been deleted .

Any help would be greatly appreciated.

TIA

Paul

User avatar
jvierra
Posts: 12087
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Multiple local account on Multiple servers erase

Post by jvierra » Tue Nov 07, 2017 7:01 am

Use ADSI to remove local accounts without the need for remoting.

There are a number of local account management modules available here: http://gallery.technet.microsoft.com

You can also load the local accounts module with OneGet:
find-module *localaccount*,localuser*
Select the module you prefer.

User avatar
chill3r
Posts: 4
Joined: Tue Nov 07, 2017 6:18 am

Re: Multiple local account on Multiple servers erase

Post by chill3r » Tue Nov 07, 2017 7:12 am

our environment has been locked down and we cannot connect to the machine any other way - it has to be PSSESSION

User avatar
jvierra
Posts: 12087
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Multiple local account on Multiple servers erase

Post by jvierra » Tue Nov 07, 2017 7:22 am

You still need to look in the Gallery for scripts modules that do what you ask.

There is no internal command that can do this except on Windows 10 with PS 5.1 swhich includes local account management CmdLets.

User avatar
jvierra
Posts: 12087
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Multiple local account on Multiple servers erase

Post by jvierra » Tue Nov 07, 2017 7:35 am

Here is the ADSI command to delete a local account on a remote system.

Code: Select all

$computername = 'RemotePC'
$userAccount = 'TestUser'
$computer = [adsi]"WinNT://$Computername"
$computer.Delete('User', $userAccount)

User avatar
chill3r
Posts: 4
Joined: Tue Nov 07, 2017 6:18 am

Re: Multiple local account on Multiple servers erase

Post by chill3r » Tue Nov 07, 2017 7:36 am

How can i get ADSI to check a spreadsheet of a lot of account names - then if it finds it remove the account.

User avatar
jvierra
Posts: 12087
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Multiple local account on Multiple servers erase

Post by jvierra » Tue Nov 07, 2017 7:42 am

You will have to write a script that opens the spreadsheet or export the sheet to a CSV and use PS CmdLets to load and read it.

User avatar
chill3r
Posts: 4
Joined: Tue Nov 07, 2017 6:18 am

Re: Multiple local account on Multiple servers erase

Post by chill3r » Tue Nov 07, 2017 8:29 am

Ok thanks for your feedback.

Locked