Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Delete all users except for some  (Read 3261 times)

0 Members and 1 Guest are viewing this topic.

Dumgui

    Topic Starter


    Newbie

    • Experience: Experienced
    • OS: Windows 7
    Delete all users except for some
    « on: May 09, 2017, 05:57:05 PM »
    I work in IT, and I have to remove users from loaner laptops all the time.  What I currently use to do this is:
    " rmdir (username) /s/q "
    rinse and repeat, but I would like to do something like
    " rmdir * /s/q " then have the exception of administrator and my username and some others. (same on every laptop)

    Any help would be much appreciated.

    Side note: I prefer this method over just deleting in windows because it doesn't ask any questions.  User profiles are full of read-only and protected files.  This method deletes everything in the folder no matter what it is.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Delete all users except for some
    « Reply #1 on: May 09, 2017, 06:25:21 PM »
    Code: [Select]
    @echo off
    pushd "%USERPROFILE%\"
    pushd ".."
    for /D %%P in (*) do if not %%P==Administrator if not %%P==MyUserName echo %%P 
    popd&popd

    the echo there is a non-destructive placeholder. You can add additional "if not %%P==Whatever" segments for additional profiles you wouldn't want to delete. Once you are happy with the list of folders it provides, you can replace the "echo %%P" with "rmdir %%P /s /q" to have it remove those folders.
    I was trying to dereference Null Pointers before it was cool.

    Dumgui

      Topic Starter


      Newbie

      • Experience: Experienced
      • OS: Windows 7
      Re: Delete all users except for some
      « Reply #2 on: May 10, 2017, 10:42:50 AM »
      Awesome this worked very well, thank you.