Solved

Terminal server question

Posted on 2016-08-24
17
54 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was asked if I could set up a fax machine so that incoming faxes were delivered to people's Exchange inboxes and so that they could send faxes from their desktops without needing to print the document first.  I knew it was possible but I had no id…
We recently had an issue where out of nowhere, end users started indicating that their logins to our terminal server were just showing a "blank screen." After checking the usual suspects -- profiles, shell=explorer.exe in the registry, userinit.exe,…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…

685 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