Solved

What powershell script to remove folder weekly?

Posted on 2015-02-18
6
61 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
[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
  • 3
  • 3
6 Comments
 
LVL 40

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 40

Expert Comment

by:footech
ID: 40635946
Please change the filter to "*.old".  That works in my testing.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 40

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import CSV with All modify groups 17 87
output in HTML format powershell 6 49
Powershell 2.0/3.0 2 42
Powershell: Combining a where statement with contains 2 20
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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