locked
Delete files older than 1 year & create text file in it's place RRS feed

  • Question

  • I have previously been using this successfully :-

    function Date-Replace([DateTime]$OlderThen, [string]$Path){
        Get-ChildItem $Path -Recurse -File | ?{($_.CreationTime) -lt $OlderThen} | %{
            new-item -ItemType File -Path $($_.Directory.FullName) -Name "$($_.Name)_$((Get-Date).ToString("dd-MM-yyyy")).txt" -Force
            $_ | Remove-Item -Force
        }
    }
    
    Date-Replace -OlderThen (Get-Date).AddYears(-1) -Path 'G:\Folder'

    However, need a few tweaks & advice as below:-

    Use ModifiedTime (not CreationTime)

    Can UNC be used instead of drive\folder?

    Require exemption of text files

    Can specify multiple folder locations to be included

    Will keep folder structure intact & get all files in all subfolders

    Many Thanks in advance, J

    Tuesday, June 4, 2019 12:37 PM

All replies