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:<\/p>\n
for /d “delims=” %%p in (\"C:\\Users) do rmdir “%%p” /s /q<\/p>\n
But I don’t know how to add the identifying .orph to the %%p so it only hits the target folders.<\/p>\n
Help much appreciated, thank you.<\/p>","upvoteCount":6,"answerCount":12,"datePublished":"2025-06-05T10:07:44.680Z","author":{"@type":"Person","name":"spiceuser-xj65","url":"https://community.spiceworks.com/u/spiceuser-xj65"},"acceptedAnswer":{"@type":"Answer","text":"
for /d %%p in (“C:\\Users*.orph”) do ( \necho Deleting folder: %%p \nrmdir “%%p” /s /q \n)<\/p>\n
for /d %%p in (\"C:\\Users\\*.orph\")<\/code> loops through all directories in C:\\Users<\/code> that end with .orph<\/code>.<\/p>\nrmdir \"%%p\" /s /q<\/code> deletes the directory and all its contents.<\/p>\necho Deleting folder: %%p<\/code> is optional but useful for debugging or logging.<\/p>","upvoteCount":2,"datePublished":"2025-06-05T13:37:44.680Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/2","author":{"@type":"Person","name":"Stark","url":"https://community.spiceworks.com/u/Stark"}},"suggestedAnswer":[{"@type":"Answer","text":"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:<\/p>\n
for /d “delims=” %%p in (\"C:\\Users) do rmdir “%%p” /s /q<\/p>\n
But I don’t know how to add the identifying .orph to the %%p so it only hits the target folders.<\/p>\n
Help much appreciated, thank you.<\/p>","upvoteCount":6,"datePublished":"2025-06-05T10:07:44.734Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/1","author":{"@type":"Person","name":"spiceuser-xj65","url":"https://community.spiceworks.com/u/spiceuser-xj65"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-xj65:<\/div>\n
\nI 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:<\/p>\n
for /d “delims=” %%p in (\"C:\\Users) do rmdir “%%p” /s /q<\/p>\n
But I don’t know how to add the identifying .orph to the %%p so it only hits the target folders.<\/p>\n
Help much appreciated, thank you.<\/p>\n<\/blockquote>\n<\/aside>\n
But are you trying to delete old user profiles ?<\/p>\n
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…<\/p>\n
This is from Win10 \nTo delete a user profile using the command line in Windows 10, you can use the net user<\/code> command to delete the user account and then use the rd<\/code> command to remove the user’s profile folder. First, open Command Prompt as administrator and run net user <username> /delete<\/code> to delete the user account. Then, run rd /s /q \"C:\\Users\\<username>\"<\/code> to remove the profile folder.<\/p>","upvoteCount":4,"datePublished":"2025-06-05T13:43:10.447Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/3","author":{"@type":"Person","name":"adrian_ych","url":"https://community.spiceworks.com/u/adrian_ych"}},{"@type":"Answer","text":"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. \nI’ll have a play with your suggestions and let you know!<\/p>","upvoteCount":1,"datePublished":"2025-06-05T14:38:29.343Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/4","author":{"@type":"Person","name":"spiceuser-xj65","url":"https://community.spiceworks.com/u/spiceuser-xj65"}},{"@type":"Answer","text":"
Thank you Stark I’ll try this and let you know!<\/p>","upvoteCount":2,"datePublished":"2025-06-05T14:39:09.635Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/5","author":{"@type":"Person","name":"spiceuser-xj65","url":"https://community.spiceworks.com/u/spiceuser-xj65"}},{"@type":"Answer","text":"
You can do old profile delete through gpo as well.<\/p>\n
Computer config/policies/admin templates/system/user profile/“delete user profiles older than a specified number of days on system restart”<\/p>\n
regards.<\/p>","upvoteCount":4,"datePublished":"2025-06-05T15:56:26.647Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/6","author":{"@type":"Person","name":"theoriginaljason","url":"https://community.spiceworks.com/u/theoriginaljason"}},{"@type":"Answer","text":"\n\n
<\/div>\n
The Original Jason:<\/div>\n
\nYou can do old profile delete through gpo as well.<\/p>\n<\/blockquote>\n<\/aside>\n
Or Powershell in lieu of Batch<\/p>","upvoteCount":2,"datePublished":"2025-06-05T18:20:25.556Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/7","author":{"@type":"Person","name":"kwelch007","url":"https://community.spiceworks.com/u/kwelch007"}},{"@type":"Answer","text":"
I second the Group Policy suggestion.<\/p>","upvoteCount":3,"datePublished":"2025-06-05T18:31:03.133Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/8","author":{"@type":"Person","name":"jameswalker20","url":"https://community.spiceworks.com/u/jameswalker20"}},{"@type":"Answer","text":"
Gotcha. You’re close, but you’ll want to filter the loop so it only targets folders that end with .orph<\/code>. Here’s a refined version of your script that should do the trick:<\/p>\nfor /d %%p in (“C:\\Users*.orph”) do rmdir “%%p” /s /q<\/p>\n
That *.orph<\/code> part does the filtering. It tells the loop to only grab directories inside C:\\Users<\/code> that end with .orph<\/code>. The rest of your command (rmdir \"%%p\" /s /q<\/code>) stays the same and will forcefully delete them without prompting.<\/p>\nA couple of quick tips:<\/p>\n
\nMake sure this .bat<\/code> runs with the right permissions, especially if any of those folders need elevated rights to delete.<\/li>\nIf 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:<\/li>\n<\/ul>\nfor /d %%p in (“C:\\Users*.orph”) do echo Deleting: “%%p”<\/p>\n
Once you’re happy with the results, switch back to the rmdir line.<\/p>","upvoteCount":1,"datePublished":"2025-06-06T16:12:04.867Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/9","author":{"@type":"Person","name":"JohnFreeman","url":"https://community.spiceworks.com/u/JohnFreeman"}},{"@type":"Answer","text":"
Thank you - that works a treat!<\/p>","upvoteCount":0,"datePublished":"2025-06-06T20:30:26.673Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/10","author":{"@type":"Person","name":"spiceuser-xj65","url":"https://community.spiceworks.com/u/spiceuser-xj65"}},{"@type":"Answer","text":"
Awesome. If the issue is resolved please mark ‘Best Answer’ <\/p>","upvoteCount":0,"datePublished":"2025-06-10T20:21:52.088Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/11","author":{"@type":"Person","name":"JohnFreeman","url":"https://community.spiceworks.com/u/JohnFreeman"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-xj65:<\/div>\n
\nWe have a process for clearing profiles from the stupidly small C: drives within our corporation.<\/p>\n<\/blockquote>\n<\/aside>\n
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.<\/p>\n
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)…<\/p>\n
\nc:\\user%username%\\desktop<\/li>\n c:\\user%username%\\downloads<\/li>\n c:\\user%username%\\documents<\/li>\n c:\\user%username%\\appdata\\local\\temp<\/li>\n c:\\user%username%\\appdata\\local\\onedrive<\/li>\n c:\\user%username%\\appdata\\local\\microsoft\\onedrive<\/li>\n c:\\Users%username%\\AppData\\Local\\Microsoft\\Terminal Server Client\\Cache<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2025-06-11T06:37:55.643Z","url":"https://community.spiceworks.com/t/help-with-batch-script-please/1212651/12","author":{"@type":"Person","name":"adrian_ych","url":"https://community.spiceworks.com/u/adrian_ych"}}]}}
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
Stark
(Stark)
June 5, 2025, 1:37pm
2
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
spiceuser-xj65:
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.
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’
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