Solved

Terminal server question

Posted on 2016-08-24
17
58 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Scenario:  You do full backups to a internal hard drive in either product (SBS or Server 2008).  All goes well for a very long time.  One day, backups begin to fail with a message that the disk is full.  Your disk contains many, many more backups th…
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
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…

690 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