Delete folders older than on network share

Dear forum readers,

I am looking for a script that deletes all folders including pdf files after 14 days.
These folders are in our local network.

Who can provide me with the complete script for this, as I do not have Powershell knowledge enough to do this.

So the script is executed from a desktop in this network.

Regards,
FvdF

FvdF,
Welcome to the forum. :wave:t4:

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Olaf,

That is clear.

I have a script that i am working on but still does not working.

Now I can’t logon anymore???

So you may show your code, explain what’s not working as expected or what errors you get and we might be able to help.

What do you mean?

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

Here is my code.

$date = get-date -Format D

<# This sets the title at the top of the log file #>
$logfile = "Log for: " + $date + "`n`n"

<# This sets the path and file name for the log file that will be exported at the end of the script #>
$logfilepath = "\\P1603\C$\Users\User\Desktop\LogPS\" + $date + " Deletion Log.txt" 

<# The counter to keep track of how many files were copied #>
[int]$totalfilescopied = 0 

<# The counter to keep track of how many files were deleted #>
[int]$totalfilesdeleted = 0 

<# This is the command that gets all of the files (PDFs in my case) that are older than 90 days existing in that folder #>
$PDFsOlderThan90Days = Get-ChildItem '\\diskstation03\werkgroep\Grafisch Bedrijf\Rotatie\Origineel' *.pdf | Where-Object {$_.LastWriteTime -le (get-date).AddDays(-14)} 

<# This is heart of the script that does all of the copying and deleting. You can see how it appends each file name to the log file by saying $logfile += .... #>

$PDFsOlderThan14Days | ForEach-Object { 
	copy $_.fullname '\\diskstation03\werkgroep\Grafisch Bedrijf\Rotatie\@FvdF\Origineel'
	$logfile += "Copied file: " + $_.Name + " to backup directory `n"
	$totalfilescopied = $totalfilescopied +1
	remove-item $_.fullname 
	$logfile += "Deleted file: " + $_.Name + " from original directory `n"
	$totalfilesdeleted = $totalfilesdeleted + 1

}

<# This writes to the log file the total number of files copied #>
$logfile += "`n`nYou copied a total of " + $totalfilescopied + " files  to the OlderThan14 folder `n" 

<# This writes to the log file the total number of files deleted #>
$logfile += "`n`nYou deleted a total of " + $totalfilesdeleted + " files from the original folder `n" 

<# This is what seals the deal and writes the log file to a .txt file #>
$logfile | out-file $logfilepath

And what is the question?

I want to delete these pdf files from this folder ```
\diskstation03\werkgroep\Grafisch Bedrijf\Rotatie\Origineel older than 14 days.


But nothing happens.

I found the problem.

There was a mismatch between PDFsOlderThan90Days and PDFsOlderThan14Days

$PDFsOlderThan90Days = Get-ChildItem ‘W:\Grafisch Bedrijf\Rotatie\FvdF\Origineel’ *.pdf | Where-Object {$_.LastWriteTime -le (get-date).AddDays(-14)}

<# This is heart of the script that does all of the copying and deleting. You can see how it appends each file name to the log file by saying $logfile += … #>

$PDFsOlderThan14Days | ForEach-Object {

Yet one question,

How can I delete also the folders older than 14 day’s? Now only the files in these folders are deleted.

You may run a second query after your script finished and look for empty folders and delete them if there are some.