I have a .bat that runs on Startup. I would like to add some extra functionality to it: to delete specific folders in C:\Users that have been renamed with something like “.orph” appended to the end. I have this for starters:

for /d “delims=” %%p in ("C:\Users) do rmdir “%%p” /s /q

But I don’t know how to add the identifying .orph to the %%p so it only hits the target folders.

Help much appreciated, thank you.

6 Spice ups

for /d %%p in (“C:\Users*.orph”) do (
echo Deleting folder: %%p
rmdir “%%p” /s /q
)

for /d %%p in ("C:\Users\*.orph") loops through all directories in C:\Users that end with .orph.

rmdir "%%p" /s /q deletes the directory and all its contents.

echo Deleting folder: %%p is optional but useful for debugging or logging.

2 Spice ups

But are you trying to delete old user profiles ?

If so, the last thing you want to do is rename or delete these folders but go to computer advanced properties and delete the profiles from there…

This is from Win10
To delete a user profile using the command line in Windows 10, you can use the net user command to delete the user account and then use the rd command to remove the user’s profile folder. First, open Command Prompt as administrator and run net user <username> /delete to delete the user account. Then, run rd /s /q "C:\Users\<username>" to remove the profile folder.

4 Spice ups

Hi adrian_ych, thank you for this. We have a process for clearing profiles from the stupidly small C: drives within our corporation. Occasionally, if a user isn’t logged into OneDrive, their C:\Users<folder> gets left behind, but it can’t be deleted without elevated permissions (beyond our level). We have found a way to use these permissions via Task Scheduler (at Startup) so I’m trying to use those at the same time to remove the orphaned folders.
I’ll have a play with your suggestions and let you know!

1 Spice up

Thank you Stark I’ll try this and let you know!

2 Spice ups

You can do old profile delete through gpo as well.

Computer config/policies/admin templates/system/user profile/“delete user profiles older than a specified number of days on system restart”

regards.

4 Spice ups

Or Powershell in lieu of Batch

2 Spice ups

I second the Group Policy suggestion.

3 Spice ups

Gotcha. You’re close, but you’ll want to filter the loop so it only targets folders that end with .orph. Here’s a refined version of your script that should do the trick:

for /d %%p in (“C:\Users*.orph”) do rmdir “%%p” /s /q

That *.orph part does the filtering. It tells the loop to only grab directories inside C:\Users that end with .orph. The rest of your command (rmdir "%%p" /s /q) stays the same and will forcefully delete them without prompting.

A couple of quick tips:

  • Make sure this .bat runs with the right permissions, especially if any of those folders need elevated rights to delete.
  • If you want to be extra cautious, you could echo out the folders it’s about to delete before actually deleting them. That way you can sanity check:

for /d %%p in (“C:\Users*.orph”) do echo Deleting: “%%p”

Once you’re happy with the results, switch back to the rmdir line.

1 Spice up

Thank you - that works a treat!

Awesome. If the issue is resolved please mark ‘Best Answer’ :slight_smile:

But you need to know the differences between clearing files from profiles vs removing the profiles ? Coz the last thing you want is having so many profiles stuck within a machine especially for Windows 11 that can cause security issues, performance issues and even application issues.

If for us (when we had shared machines for Domain users), I would delete contents in the following folders only and leave the c:\user%username% intact (especially some of the files)…

  • c:\user%username%\desktop
  • c:\user%username%\downloads
  • c:\user%username%\documents
  • c:\user%username%\appdata\local\temp
  • c:\user%username%\appdata\local\onedrive
  • c:\user%username%\appdata\local\microsoft\onedrive
  • c:\Users%username%\AppData\Local\Microsoft\Terminal Server Client\Cache