Avatar of Rein Christensen
Rein Christensen
 asked on

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?
Windows Server 2008

Avatar of undefined
Last Comment
Itxx

8/22/2022 - Mon
Nick Rhode

You would use %username%
Rein Christensen

ASKER
So-  cd C:\Users\%username%\downloads\
       Del *.* /Q
Rein Christensen

ASKER
Or what would be a good command to use?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Nick Rhode

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
Rein Christensen

ASKER
Is there any other way? besides that? Like running a script on a weekly schedule to do this?
ASKER CERTIFIED SOLUTION
Nick Rhode

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rein Christensen

ASKER
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\*.*
}
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rein Christensen

ASKER
The file is in c:\users\shipping\downloads
Rein Christensen

ASKER
When I run the stuff individually in powershell it works fine. So must just be a spacing issue or something.
Rein Christensen

ASKER
Any help would be appreciated.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Itxx

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rein Christensen

ASKER
That was it. Thank you very much Itxx and thank you Nick as well for providing the powershell command.
Rein Christensen

ASKER
Worked like a charm.
Rein Christensen

ASKER
Is there a way to have it always set as unrestricted?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Itxx

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
Rein Christensen

ASKER
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.
Itxx

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy