Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

What powershell script to remove folder weekly?

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
xouvang
Asked:
xouvang
  • 3
  • 3
1 Solution
 
footechCommented:
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
 
xouvangAuthor Commented:
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
 
footechCommented:
Please change the filter to "*.old".  That works in my testing.
0
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
xouvangAuthor Commented:
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
 
footechCommented:
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
 
xouvangAuthor Commented:
thank you very much!
0

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now