Solved

Terminal server question

Posted on 2016-08-24
17
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

732 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