Solved

Terminal server question

Posted on 2016-08-24
17
42 Views
Last Modified: 2016-08-25
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?
0
Comment
Question by:Rein Christensen
  • 11
  • 3
  • 3
17 Comments
 
LVL 22

Expert Comment

by:Nick Rhode
ID: 41769054
You would use %username%
1
 

Author Comment

by:Rein Christensen
ID: 41769071
So-  cd C:\Users\%username%\downloads\
       Del *.* /Q
0
 

Author Comment

by:Rein Christensen
ID: 41769075
Or what would be a good command to use?
0
 
LVL 22

Expert Comment

by:Nick Rhode
ID: 41769079
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
 

Author Comment

by:Rein Christensen
ID: 41769098
Is there any other way? besides that? Like running a script on a weekly schedule to do this?
0
 
LVL 22

Accepted Solution

by:
Nick Rhode earned 250 total points
ID: 41769117
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
 

Author Comment

by:Rein Christensen
ID: 41769186
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
 

Author Comment

by:Rein Christensen
ID: 41769196
The file is in c:\users\shipping\downloads
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Rein Christensen
ID: 41769360
When I run the stuff individually in powershell it works fine. So must just be a spacing issue or something.
0
 

Author Comment

by:Rein Christensen
ID: 41770229
Any help would be appreciated.
0
 
LVL 1

Assisted Solution

by:Itxx
Itxx earned 250 total points
ID: 41770267
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
 

Author Comment

by:Rein Christensen
ID: 41770422
That was it. Thank you very much Itxx and thank you Nick as well for providing the powershell command.
0
 

Author Closing Comment

by:Rein Christensen
ID: 41770425
Worked like a charm.
0
 

Author Comment

by:Rein Christensen
ID: 41770441
Is there a way to have it always set as unrestricted?
0
 
LVL 1

Expert Comment

by:Itxx
ID: 41770468
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
 

Author Comment

by:Rein Christensen
ID: 41770474
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
 
LVL 1

Expert Comment

by:Itxx
ID: 41771256
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you migrate a Terminal Server licenses server inside the 2008 server family, you can takte advantage of the build-in migration tool. If you like to migrate an older 2003 Server (and the installed client CALs) to a 2008 R2 server for example, you …
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now