Multiple local account on Multiple servers erase

Ask your 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.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
chill3r
Posts: 4
Last visit: Thu Nov 16, 2017 6:19 am

Multiple local account on Multiple servers erase

Post by chill3r »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple local account on Multiple servers erase

Post by jvierra »

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
Last visit: Thu Nov 16, 2017 6:19 am

Re: Multiple local account on Multiple servers erase

Post by chill3r »

our environment has been locked down and we cannot connect to the machine any other way - it has to be PSSESSION
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple local account on Multiple servers erase

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple local account on Multiple servers erase

Post by jvierra »

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
Last visit: Thu Nov 16, 2017 6:19 am

Re: Multiple local account on Multiple servers erase

Post by chill3r »

How can i get ADSI to check a spreadsheet of a lot of account names - then if it finds it remove the account.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple local account on Multiple servers erase

Post by jvierra »

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
Last visit: Thu Nov 16, 2017 6:19 am

Re: Multiple local account on Multiple servers erase

Post by chill3r »

Ok thanks for your feedback.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked