Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Auto delete everything but specified folder contents from txt file

$
0
0

Hey Guys,

I am brand new to using Powershell (like 48 hours into using it for work) and I've run into a bit of an issue.  I've only taken a programming concept classes so some of the stuff makes sense to me but a lot of it is new.  Basically I've made a script that automatically deletes any files over X amount of days.  My next step is to have an exceptions text that will have a list of folders that should not have its contents deleted.  This is what I have so far.


$Date= Get-Date 
$Days = "7"
$Folder = "C:\publicftp\users"
$LastWrite = $Now.AddDays(-$Days)
#----- getting DO NOT DELETE listing from TXT ----#
$Exceptions = Get-Content "c:\exclude.txt"
$Files = Get-Childitem $Folder -Recurse -Exclude "c:\exclude.txt" | Where {$_.LastWriteTime -le "$LastWrite"}


foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        Remove-Item $File.FullName -Exclude $Exceptions | out-null
        }
    }

I've seen a lot of threads that show how to auto delete contents or how to exclude specific file types but I haven't seen an answer to my particular problem.  Any help would be greatly appreciated!  Thanks!




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>