1

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
ShadowSF96
  • 65
  • 2
  • 2
  • 10

1 Answers1

3

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"
  )
)
Kokkie
  • 546
  • 6
  • 15
  • Thanks Kokkie. This method seems to work nicely, I appreciate it. However, would it be possible to do a thing similar to this, but in a command prompt? We already run commands from the command prompt when installing software on users' computers, and being able to combine this with what we already have would be most optimal. However, I'll definitely save this, thanks again! – ShadowSF96 Aug 26 '14 at 08:15
  • Thank you so much, Kokkie. You've been a great help, and for that I am grateful. I did a tiny modification to your MS-DOS command, and I found this one to do EXACTLY what I want it to: http://pastebin.com/ivZz1niG - it says Cisco instead of Temp because we need to delete a preferences file in the Cisco folder. Thank you again. – ShadowSF96 Aug 26 '14 at 10:19