Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Remove folders in a directory but keep the three latest (creation date)

$
0
0

Hi admins!

Im very new to powershell but ive heard that this is the place to ask questions/learn powershell.
I have a website where code is deployed to a directory. (d:\websites)
Every time some developer deploys an updated site a new directory is created and the IIS points to this new directory.
d:\websites\ver1
d:\websites\ver2
d:\websites\ver3
d:\websites\ver4
and so on……..

I would like to schedule powershell to delete all folders and files in this directory except the last 2 based on creation date. (Because eventually the disk will be filled up)
Understand that you are not supposed to just ask for a complete script here but im just starting to learn powershell :)

What i have so far is:

$path = "d:\websites"
$files = Get-ChildItem -Path $path -Recurse | Where-Object { $_.PsIsContainer -eq "$true"}

How would you guys solve this task?

Regards,
Johan


Viewing all articles
Browse latest Browse all 13067

Latest Images

Trending Articles



Latest Images