Terminal server question

I have a terminal server running Windows 2008 R2 with about 100 users.  Each user has a "downloads" folder.  I need to schedule a weekly batch file to delete the files inside of this folder for all users.

 For one user, I can use this simple batch file:
      cd C:\Users\<username>\downloads\
      Del *.* /Q

 Is it possible to delete the contents of the "Downloads" folder for all users - all folders that are in C:\Users?
Rein ChristensenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nick RhodeIT DirectorCommented:
You would use %username%
1
Rein ChristensenAuthor Commented:
So-  cd C:\Users\%username%\downloads\
       Del *.* /Q
0
Rein ChristensenAuthor Commented:
Or what would be a good command to use?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Nick RhodeIT DirectorCommented:
That would be a tag you would add in group policy or attach as a logoff script for all your users using the RDP Server.  That way it runs on each user that signs into the system.  %username% targets the current user logged in
0
Rein ChristensenAuthor Commented:
Is there any other way? besides that? Like running a script on a weekly schedule to do this?
0
Nick RhodeIT DirectorCommented:
You can use powershell to do it right away for all users and user it as a scheduled task on the server so it runs weekly

$username = gci C:\users
foreach ($user in $username)
{ remove-item c:\users\$user\downloads\*.*
}

Copy it to notepad and save it as .ps1

Right-click and run as powershell
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rein ChristensenAuthor Commented:
I tried running it in powershell, however a test file that I put there on purpose in a user folder is not getting deleted.
This is the file I am using:
$username = gci C:\users
foreach ($user in $username)
{ remove-item c:\users\$user\downloads\*.*
}
0
Rein ChristensenAuthor Commented:
The file is in c:\users\shipping\downloads
0
Rein ChristensenAuthor Commented:
When I run the stuff individually in powershell it works fine. So must just be a spacing issue or something.
0
Rein ChristensenAuthor Commented:
Any help would be appreciated.
0
ItxxCommented:
Could it be that your Powershell executionpolicy is not correctly set?

Either try in Powershell: set-executionpolicy unrestricted and try running the script again.

Or Start -> Run: powershell.exe bypass -file path/to/script.ps1
0
Rein ChristensenAuthor Commented:
That was it. Thank you very much Itxx and thank you Nick as well for providing the powershell command.
0
Rein ChristensenAuthor Commented:
Worked like a charm.
0
Rein ChristensenAuthor Commented:
Is there a way to have it always set as unrestricted?
0
ItxxCommented:
I don't think so but you might be able to do it if you make a .bat file that calls powershell.exe -executionpolicy bypass -file path/to/script.ps1
0
Rein ChristensenAuthor Commented:
The only reason I am asking, is when I set up a windows scheduled task. It does not run the file, however when I right click the file and do "Run with PowerShell" it works. Now I have it set to "Open with" Notepad, however if I change it to "Open With" Powershell it does not work.
0
ItxxCommented:
What happens if you create a .bat file that says: Powershell.exe -executionpolicy bypass -File  path/to/script.ps1

It should run fine from task scheduler
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.