I'm unsure if this is exclusively a unix/linux scripting forum but I need help writing a windows script (I'm more of a perl/unix scripter).

I need to be able to periodically clean out the user profiles on a presentation PC in a conference room. Therefore I need a script which will remove directories recursively. However I don't know the name of the directories, except for a prefix. I know that windows rd /s /q won't do it, because it won't allow the use of wildcards (which is the obvious way to do it but being Micro$oft of course... :( ).

Therefore, is there someone who could supply me with a command that will remove directories and all the subdirectories under them, in windows? Pretty please?

Dani AI

Generated

A few practical notes that build on 's batch idea and address the error saw.

The "%dir was unexpected at this time" message almost always means the FOR token was misused: interactive cmd.exe uses a single percent (for example %i), batch files require doubling that sign (%%i), and the FOR variable itself is a single character (so %%i, not %%dir). That is why the loop failed the first time. If you stay in batch scripting, test the loop by echoing the variable first so you see the exact names being passed before you delete anything.

A safer, simpler approach on modern Windows is PowerShell (available as an add-on on XP, native from Vista/7 onward). Example preview and delete sequence (adjust path for XP vs Vista+):

# Preview the folders that match
Get-ChildItem 'C:\Users\Prefix*' -Force | Where-Object { $_.PSIsContainer } | Select-Object FullName

# When the preview looks right, delete
Get-ChildItem 'C:\Users\Prefix*' -Force | Where-Object { $_.PSIsContainer } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

Run PowerShell elevated and make sure no user is logged in to the profiles you plan to remove; locked files will block deletion.

A few important cautions and alternatives:

  • Deleting profile folders alone can leave orphaned registry entries under HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList. Use the Users control panel "Delete" or tools like DelProf/DelProf2 to remove both files and the profile registry keys.
  • Test thoroughly first (preview output, run on a non-production machine).
  • Consider moving folders to an archive location rather than immediate deletion while you validate the results.

This gives a reliable, repeatable workflow: enumerate (preview), ensure no in-use profiles, delete with a tool that suits your OS, then clean registry entries if necessary.

Recommended Answers

All 3 Replies

You'll have to enable command extensions and use the for command. Works on XP.

:: change to the proper directory (probably C:\Documents and Settings\)
%SystemDrive%
cd "%ALLUSERSPROFILE%\.."

:: for each subdirectory name except "Administrator" and "All Users" do delete it
for /D %%dir in (*) do (
  if not "%%dir"=="Administrator" (
    if not "%%dir"=="All Users" rd /s/q "%%dir"
    )
  )

This is untested... you may have to tweak it some.

Good luck

You'll have to enable command extensions and use the for command. Works on XP.

:: change to the proper directory (probably C:\Documents and Settings\)
%SystemDrive%
cd "%ALLUSERSPROFILE%\.."

:: for each subdirectory name except "Administrator" and "All Users" do delete it
for /D %%dir in (*) do (
  if not "%%dir"=="Administrator" (
    if not "%%dir"=="All Users" rd /s/q "%%dir"
    )
  )

This is untested... you may have to tweak it some.

Good luck

Thanks. If I read that correctly, that will pass the value of %%dir, which is "*" to the command "rd", right?

Unfortunately rd, like rmdir, refuses to accept wildcards.

Its very elegant but all I get is the error:

%dir was unexpected at this time.

You are going to have a hard time shell scripting if you are unwilling to read about basic commands like for. Rd won't see "*" unless you somehow manage to name a directory "*".

I did also mention that basic testing was needed... which I have now done. Change every %%dir to read %%d . Apparently the command interpreter didn't like me using a variable name with more than one letter to its name.

Also, you might want to add @echo off to the top of the script so you don't see every command as it is executed.

VERY IMPORTANT
And before you run it make sure that you get the "Administrator" and "All Users" and any other directory you don't want deleted in there spelled correctly and capitalized correctly. If not you'll regret it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.