Solved

What powershell script to remove folder weekly?

Posted on 2015-02-18
6
58 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now