Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What powershell script to remove folder weekly?

Posted on 2015-02-18
6
Medium Priority
?
63 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 41

Accepted Solution

by:
footech earned 2000 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 41

Expert Comment

by:footech
ID: 40635946
Please change the filter to "*.old".  That works in my testing.
0
Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

 

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 41

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

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

705 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