Identify and remove local account
We have a windows 7 scripted image that creates a user called SciptedUser. We often forget to remove that account before moving the system to production.
Anybody know of an easy way to use the K1000 to locate systems with that account, and then have it remove the account?
0 Comments
[ + ] Show comments
Answers (4)
Please log in to answer
Posted by:
rileyz
8 years ago
*cough...
$UserProfile = Get-WmiObject Win32_UserProfile -filter "localpath='C:\\Users\\$User'"
$DebugPreference = 'Continue' #Continue|SilentlyContinue$LocalAccountsArray = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select Name#https://mcpmag.com/articles/2015/04/15/reporting-on-local-accounts.aspx$RemoveThisUser = 'SciptedUser'Foreach ($User in $LocalAccountsArray) {$User = ($user.name) #Convert psobject to string Write-debug "Working on $User" If ($User -eq "$RemoveThisUser") {Write-Warning "Removing account $User" &net User `"$user`" /Delete}}
Blah its all jibberish, try here - url expires on 30/09/16.
http://pastebin.com/Z1bW1jrA
http://pastebin.com/Z1bW1jrA
You'll need to run this via PowerShell on the systems. If you are copying and pasting, make sure your double and singles are proper ones! Not the angled ones. Sometimes when its published online they mangle the quotes.
*Addition
If you want to remove the profile then add code block below before the &net User `"$user`" /Delete line. Edit in PowerShell ISE as it will let you know if the syntax is wrong.
$UserProfile = Get-WmiObject Win32_UserProfile -filter "localpath='C:\\Users\\$User'"
$UserProfile.Delete()
*Updated
I rewrote it, please see the URL below. Instead of using the foreach loop I decided it would be better to use the contains method - more efficient than processing every record in the array. Anyway here it is..
ps. not my fault if it blows up your computer :D
Posted by:
flip1001
8 years ago
There are 2 steps required: Remove the user profile and delete the user account.
Delprof2 – User Profile Deletion Tool can be used for the first part, but you may have to pay for it. Look at its website for details.
Here is a batch file you can use with delprof2 attached to an offline Kscript.
Delprof2 – User Profile Deletion Tool can be used for the first part, but you may have to pay for it. Look at its website for details.
Here is a batch file you can use with delprof2 attached to an offline Kscript.
IF EXIST "C:\USERS\SciptedUser\." (
REM Delete profile for ScriptedUser
delprof2.exe /u /id:SciptedUser
REM Delete the account on the local machine
NET USER SciptedUser /DELETE
)
Comments:
-
You can do it for free (:
See my comment above. - rileyz 8 years ago-
Thanks for the updated script. - flip1001 8 years ago
-
flip1001,
Thanks for the input! I was able to use your batch file recommendation as a test, and it worked great! But they want too much for 'delprof2' and I'm sure my manager will not fund it. Super easy solution though. - MichAda 8 years ago
Posted by:
MichAda
8 years ago
Posted by:
MichAda
8 years ago
How should I set up the power shell script on KACE?
I used method 2 from knowledge base 138389 but keep getting an error:
Missing expression after unary operator '-'.At line:1 char:2
The command I'm using in "Parameters" is:
-nologo -executionpolicy bypass -noprofile -file "FileName.ps1"