Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: User Account - Delete

21 Nov 2015   #10
JohnGray

Windows 7 Pro 64-bit
 
 

I am looking for a command line command to delete a Windows 7 user account from a PC in a workgroup, removing not only the user account but the associated profile C:\Users\username and (presumably) and registry entries associated with the user, just as Control Panel -> User Accounts does.

NET USER username /DELETE appears only to remove the user account, but not its files, etc.

I want to run this in a BATch file on a whole bunch of PCs, hence the request for a command line tool.

Thanks!


My System SpecsSystem Spec
.
21 Nov 2015   #11
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

warning   Warning
Users are advised to use the commands indicated in the tutorial rather than the command mentioned below.

JohnGray, there exists a WMI method that does exactly what you're after. The below command will delete all traces of the specified user account name, and will also permanently remove the user account's associated user profile folder. An Elevated Command Prompt must be used for this command.
Code:
wmic path Win32_UserProfile Where "LocalPath Like '%%\\Pyprohly'" Delete
If you decide to delete a user account using the old Net User command, remnants of the user account will be left behind on the machine. This is evident when you try to recreate a user account using a name that once existed and was deleted using the Net User command: the user account profile folder will not be named the same name the user is.

For example, if you create a user account named 'Example', login to Example, logout, delete Example with the Net User command, then recreate the user account with the same name again, the user folder will not be "C:\Users\Example" but something similar to "C:\Users\Example.COMPUTERNAME-PC". Repeating the process once will result in Example's user profile folder being "C:\Users\Example.COMPUTERNAME-PC.000". Repeating again, the folder will be "C:\Users\Example.COMPUTERNAME-PC.001". Repeating another time, the folder will be "C:\Users\Example.COMPUTERNAME-PC.002", and so on.

If you are looking for a command that removes a user account like the Control Panel does, the above Wmic command is the closest you can get. It will permanently delete the user profile though, so be extremely careful.
My System SpecsSystem Spec
21 Nov 2015   #12
JohnGray

Windows 7 Pro 64-bit
 
 

Thank you very much! That looks exactly what I want.

Could you kindly confirm that the "like" verb requires an exact match, and thus the WMIC command would not also delete (in your example) a user name starting with Pyprohly, such as PyprohlysAccount - thanks again.

Oh, and if I am setting an environment variable to contain an account name, do I double the % signs in the command?
That is:
Code:
set acct=Pyprohly
wmic path Win32_UserProfile Where "LocalPath Like '%%\\%%acct%%'" Delete
:: or just
wmic path Win32_UserProfile Where "LocalPath Like '%%\\%acct%'" Delete
My System SpecsSystem Spec
22 Nov 2015   #13
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by JohnGray View Post
Could you kindly confirm that the "like" verb requires an exact match, and thus the WMIC command would not also delete (in your example) a user name starting with Pyprohly, such as PyprohlysAccount
In my example command, "Pyprohly" is being matched exactly, but it doesn't need to be. You may place wildcards on either side of "Pyprohly" so that matches are non-exact.

So what is the wildcard character I hear you say? I was reluctant to reveal what the wildcard character is, in concern that someone may be tempted to try out the command line below...

warning   Warning
The below command is NOT to be run. A certain permanent loss of personal files will result in doing so. (The below command line is copy paste proof for users' safety.)

Code:
wmic path Win32_UserProfile Where "LocalPath Like '%'" Delete
As you can see, the wildcard character is the percent symbol "%". N.B., the above command will only work on the Command Prompt. If using batch, the percent must be doubled. If you just always double the percent symbols here anyway, you'll have a command that works both on the Command Prompt as well as in batch files.


Quote   Quote: Originally Posted by JohnGray View Post
Oh, and if I am setting an environment variable to contain an account name, do I double the % signs in the command?
That is:
Code:
set acct=Pyprohly
wmic path Win32_UserProfile Where "LocalPath Like '%%\\%%acct%%'" Delete
:: or just
wmic path Win32_UserProfile Where "LocalPath Like '%%\\%acct%'" Delete
Well your first Wmic command will do a non-exact match of "acct" (because, in batch, double percent symbols cancel to yield a single literal percent symbol, which is the wildcard character in WMI queries), while your second Wmic command will do an exact match of "Pyprohly", which is probably the functionality you are after.


I forgot to mention, there is a caveat to this method of deleting user accounts: you actually need to specify the name of the user account's profile folder, rather than the name of the user account being deleted. Hence, I think you can work out why the need for "%%\\" is required, following the account name.
My System SpecsSystem Spec
.

23 Nov 2015   #14
JohnGray

Windows 7 Pro 64-bit
 
 

Thank you yet again. I find WMIC syntax rather opaque!
My System SpecsSystem Spec
Comment

 User Account - Delete




Tutorial Tools Search this Tutorial
Search this Tutorial:

Advanced Search




Similar help and support threads
Windows 7 Tutorial Category
Not able to delete a user account
Hi, i'm the admin of my laptop. recently i created a standard acc for my friends to access my laptop. but now that acc is changed to admin and now i'm not able to change its type or delete that acc as if it has some high privilege. i try doing the same using the built in admin acc and result is...
General Discussion
Cannot delete a second administrative user account
I have two user accounts "Bob" "Delete Me" Both are administrative accounts; I just installed "Delete Me" today and no longer need it. Neither has a password. While logged on as "Bob" I tried to delete the user account, and failed. I then searched this forum for help, and got this...
Installation & Setup
ASP.NET user account... delete or disable it?
I reinstalled and updated Windows. Then created a back-up image. Whenever reinstalling from this image, I see an extra User account called ASP.NET My understanding it that it has something to do with the Net4 framework installation (perhaps Windows was not fully updated before I made the ...
Installation & Setup
Delete Account Function Missing I Cannot Delete Temporary User Account
Good Day Everyone a few hours ago, I just finished installing Windows Professional x32 and had a problem with deleting the windows.old because of the permission bug. Well I managed to delete the Windows.old by editing my security and user however when I log-offed, I saw another user account...
General Discussion
How to delete a user's account.
On my wife's machine she was having all types of problems, so I backed up her user directory and all it's data. I then went to UAC and deleted her standard user account, clicking yes to delete all folders and files. Just leaving my admin account and a guest account. After a defrag and reboot her...
Performance & Maintenance
user account that can install but not delete
Is it possible to create an adminstrator account inwindows 7 that can install but not delete programs?
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 07:53.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App