Delete a common folder from all users local AppData

21.8k views Asked by At

Is there a variable for all users in Windows 7 / 8? For example, let's say every user on the PC has a specific folder on their desktop, and I'd like to delete all these folders at once via a command that can be executed through command prompt, is this possible?

A script has put a preferences file for our VPN client in each our users local AppData, and this makes the VPN client automatically put in an address when you start up the client. Problem is that this address is now outdated, and we use a new one. So I'd like to find a way to execute a command that deletes all these preferences.xml files for all our users.

I've tried Googling it, but I don't think there is a variable for all users. But thought I'd ask here to be sure. I had hoped something along the lines of 'del C:\users\%ALLUSERS%\AppData\Local\example\preferences.xml' would work, but it appears after Googling it there's no such thing.

1

There are 1 answers

2
Kokkie On BEST ANSWER

You could delete all items under the userprofile temp-directory using powershell. See below solution and remove the Whatif option if you are satisfied with the actions that will be taken;

$users = Get-ChildItem c:\users | ?{ $_.PSIsContainer }
foreach ($user in $users){
    $userpath = "C:\Users\$user\AppData\Local\Temp"
    Try{
        Remove-Item $userpath\* -Recurse -Whatif -ErrorVariable errs -ErrorAction SilentlyContinue  
    } 
    catch {
        "$errs" | Out-File c:\temp\errors.txt -append
    }
}

For a MS-DOS solution, try this in a command prompt:

for /f "delims=|" %f in ('dir /B /A:D-H-R c:\users') do echo "C:\Users\%f\AppData\Local\Temp\*"

In a batchfile that will actually delete those files and directories, it would become:

for /f "delims=|" %%f in ('dir /B /A:D-H-R c:\users') do (rmdir "C:\Users\%%f\AppData\Local\Temp\" /s/q || del "C:\Users\%%f\AppData\Local\Temp\*" /s/q)
if not errorlevel 1 (
    echo "Finished with errors"
  ) else (
    echo "Finished without errors"
  )
)