user1926332
user1926332

Reputation: 715

PowerShell script to echo files from folders and subfolders and then delete files over X days old

I am curious to understand the possible ways to echo the files in folders and subfolders and generate a output stating the filenames, which are picked up to delete X days old.

I wanted to write this script in two different levels

Level1: PowerShell script only to echo filenames and give me the output of the files, which have been identified to be deleted. This should include the files including folders and subfolders.

Level2: Combine the level1 script by adding a delete functionality, which would delete the files in folders and subfolders.

I have a move script and a direct script to delete but I want to ensure the correct files are picked and I want to know the file names which are being deleted.

Any help is highly appreciated.

EDIT Added from comment

I have been trying something like this in a very simple fashion

Get-ChildItem -Path c:\test | where {$_.lastWriteTime -lt (Get-Date).addDays(-60)} 

I would like to add some parameter, which would generate an output of filenames in a different folder location.

Upvotes: 0

Views: 5898

Answers (2)

Matthew Shea
Matthew Shea

Reputation: 567

The question is a little vague, but I assume this is something like what you want.
I like David Martin's answer, but it may be a little too complex depending on your skill level and needs.

param(
    [string]$Path,
    [switch]$LogDeletions
)

foreach($Item in $(Get-ChildItem -Path $Path | where {$_.lastWriteTime -lt (Get-Date).addDays(-60)}))
{
    if($LogDeletions)
    {
        $Item | Out-File "C:\Deleted.Log" -Append
    }
    rm $Item
}

Upvotes: 0

David Martin
David Martin

Reputation: 12248

I think this is something along the lines of what you need, I have introduced you to a few concepts which you might not be aware of, such as cmdletbinding which allows you to dry run your script using the -whatif parameter. You can also supply -verbose to see what is happening along the way, you could also append to a log at this point using the Add-Content cmdlet.

So you might run it like this:

.\DeleteOldFiles.ps1 -Path c:\test -Age 50 -WhatIf -Verbose

Then when you are ready to delete the files you can run it without the -WhatIf parameter:

.\DeleteOldFiles.ps1 -Path c:\test -Age 50 -Verbose

This doesn't answer all your questions, but should help you get started, I've put plenty of comments in the code so you should be able to follow it all.

# Add CmdletBinding to support -Verbose and -WhatIf 
[CmdletBinding(SupportsShouldProcess=$True)]
param
(
    # Mandatory parameter including a test that the folder exists       
    [Parameter(Mandatory=$true)]
    [ValidateScript({Test-Path $_ -PathType 'Container'})] 
    [string] 
    $Path,

    # Optional parameter with a default of 60
    [int] 
    $Age = 60   
)

# Identify the items, and loop around each one
Get-ChildItem -Path $Path | where {$_.lastWriteTime -lt (Get-Date).addDays(-$Age)} | ForEach-Object {

    # display what is happening 
    Write-Verbose "Deleting $_ [$($_.lastWriteTime)]"

    # delete the item (whatif will do a dry run)
    $_ | Remove-Item
}

Upvotes: 1

Related Questions