Solved

Purging local accounts with powershell

Posted on 2015-01-06
4
89 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
  • 2
4 Comments
 
LVL 69

Accepted Solution

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

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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