Solved

What powershell script to remove folder weekly?

Posted on 2015-02-18
6
59 Views
Last Modified: 2015-02-27
Hi all,

I'm new to powershell and learning as I go as a JR Sys Admin. Currently looking for powershell classes to take locally but limited in AK. (Also if you're from AK and know any local class or teach powershell please let me know)

I want to run this script weekly

What I need it to do is remove folders with the file name ".old" in a certain directory.
Let's say on server1 in the C\Users there are a few profiles that had to be rebuilt and the old ones were renamed to xxx.old. Is there a script to go in and remove any folder that has the .old?

I have 7 servers, I was wondering if one script can go into those 7 servers and remove them.


Thanks
0
Comment
Question by:xouvang
  • 3
  • 3
6 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40617962
You may want to set up a scheduled task on each server to do this.  If you run a script to delete the folder over the network, then whichever account you use has to have permissions and you'll generally work via UNC path.
An example script would be (remove the -whatif parameter from Remove-Item to run for real)
$servers = "server1","server2","server3"
foreach ($server in $servers)
{
    Get-ChildItem \\$server\c$\Users -Filter ".old" | Where { $_.PsIsContainer } |
     Remove-Item -Recurse -WhatIf
}

Open in new window


Your scheduled task would be set with an Action of "Start a program", with the program being "powershell.exe", and the arguments "-executionpolicy remotesigned -noninteractive -file somescript.ps1" (the first two might be unnecessary).
0
 

Author Comment

by:xouvang
ID: 40635923
I'm testing the powershell script and it's not working.
Before I do it on thes server I'm testing it on a local machine.

So far the command works  up to Get-ChildItem \\$server\c$\Users. After the filter, nothing comes back.
Below is a screen shot of the command.

Sorry again, I'm still learning.

Capture.JPG
0
 
LVL 39

Expert Comment

by:footech
ID: 40635946
Please change the filter to "*.old".  That works in my testing.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:xouvang
ID: 40635976
Thank you that worked.

The command works but it's not removing the folder. The folder is still there. Is there another command I need to run?
Attached is the powershell screenshot

Capture.JPG
0
 
LVL 39

Expert Comment

by:footech
ID: 40636001
Yes, as I mentioned in my first post, remove the -whatif parameter from Remove-Item to run for real.  I included that so you could see what it was going to do without actually removing anything.
0
 

Author Comment

by:xouvang
ID: 40636034
thank you very much!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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