Solved

Purging local accounts with powershell

Posted on 2015-01-06
4
94 Views
Last Modified: 2015-01-06
I came across this http://richardspowershellblog.wordpress.com/2012/06/08/working-with-profiles-2-deleting-profiles/ and I like the approach it removes all accounts except the administrator account but when I try I get Get-CimInstance error. How can I correct this and how exactly do I run it against multiple systems?

A modified version I tried but get the same error

Get-CimInstance -ClassName win32_group -Filter "name = 'administrators'" | 
Get-CimAssociatedInstance -Association win32_groupuser |
Where-Object { $_.SID -notlike "*-500" } | 
ForEach-Object {
    ([ADSI]"WinNT://.").delete("user",$_.Name)
}

Open in new window

0
Comment
Question by:stlhost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40534009
Get-CimInstance requires PowerShell 3 or later.
0
 
LVL 40

Expert Comment

by:footech
ID: 40534140
It also communicates using the WS-Man protocol by default.  So unless you have PS Remoting setup on systems it won't work.  To get around this you can configure CIM sessions with the option to use DCOM, or just use the WMI cmdlets like Get-WmiObject.

Since you don't mention what the error is I can only guess.

It's unclear what exactly you're trying to do and whether you have a specific question.  Your title mentions local accounts, but the blog post is specifically not for local accounts and is in fact meant for deleting only a specified profile.
0
 
LVL 2

Author Comment

by:stlhost
ID: 40534151
Qlemo was correct I needed powershell 3. What I am wanting to do is purge local accounts except the admin account on remote workstations. There are random accounts and to many workstations to go through manually to check.
0
 
LVL 2

Author Closing Comment

by:stlhost
ID: 40534254
I did need powershell 3. The rest I figured out thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question