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
Solved

Terminal server question

Posted on 2016-08-24
17
53 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import groups from "Member Of" of user to a notepad. 4 56
extended monitor print screen 8 50
Windows Password recovery 7 52
powershell script 9 63
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…
OfficeMate Freezes on login or does not load after login credentials are input.
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…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

840 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