Script to move old modified files from before 2013 into new folder? - Powershell ISE

Thomas D. Bauer 1 Reputation point
2022-04-22T22:26:21.583+00:00

Hi so I'm currently faced with a task to look for files with a modified date from before 2013 and I'm supposed to move each old file into a brand new folder which I believe I created through this query. I'm not exactly sure if these syntaxes are running as they should but I'm not receiving any errors when I run the script. I'm running as an Administration on Powershell ISE. If anyone could please help suggest any commands that I could use or give me any recommendations that I could do to my current script.

`#If running as a user set Execution Policy scope to CurrentUser under Administration
Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
"Set-ExecutionPolicy -Scope CurrentUser"

Look for Files that have a mod date older than X date (maybe like before 2013) from a folder and its subfolders

Get-ChildItem | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-365 * 7)}  

Define a date to del before

 Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object   
 {($_.LastWriteTime -lt (Get-Date).AddDays(-365 * 7))} | Remove-Item  

Start looking through folders

Get-ChildItem -Path C:\ -Force -Recurse  

Create new folder to copy and move modified files

New-Item -Path 'C:\temp\New Folder' -ItemType Directory   
#if file is older than DATE Variable Move it to somewhere   

Copy-Item | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-365 * 7)} move-item -destination 'C:\temp\New Folder'
`

195771-capture.png

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,205 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,277 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 32,076 Reputation points
    2022-04-23T13:49:32.957+00:00

    The code sample that you posted all runs together. There are no line breaks.

    So based on your image...

    Line 7 just lists files that match the criteria in whatever current directory you have set (no subdirectories). That should work, but might not produce any output if no files match the criteria.

    Line 10/11 gets all the files that match the criteria and then DELETES THEM with a Remove-Item. So if you have run this script multiple times and are getting no output, it's because you deleted all the files you wanted to move.

    Line 14 lists all files and folders on the C:\ drive. Why? What is the purpose of that statement?

    Line 17 should actually work. But if you run the script more than once you will get an error. You might want to add a Test-Path cmdlet to see if the folder already exists before you issue the New-Item.

    Line 19 is just all wrong. You are piping a Copy-Item to a Where-Object. But Copy-Item does not generate any object to analyze. And you are missing the pipe character, "|", before the Move-Item cmdlet. Line 19 can't possibly work. It looks like you just copied and pasted some commands with no real understanding of what they are doing.

    Based on line 19, I would suggest that you review this document, and learn more about Powershell.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    While you are testing, add the -WhatIf switch to any cmdlet that deletes or moves files. Powershell will then generate a message telling you what it would do without really doing it. That way you can rerun the script multiple times to verify functionality.

    Fix your script and post the updated code.

    0 comments No comments

  2. Limitless Technology 39,391 Reputation points
    2022-04-28T07:48:58.16+00:00

    Hi ThomasDBauer-9832,

    The script you have provided looks as if it should work on the face of it. It's only looking back 7 years (365 * 7) so that isn't 2013, but 2015.

    I would suggest a different script based on the following example:

    do {
    $date = Read-host "Enter date (MM/DD/YYYY) : "
    } while ($date -as [datetime] -isnot [datetime])
    $date = $date -as [datetime]
    $date

    New-Item -Path 'C:\temp\New Folder' -ItemType Directory

    $path = "C:\temp\others\"
    $destination = new-item c:\temp\others\$($date.toshortdatestring().replace("/","-")) -type directory
    Foreach($file in (Get-ChildItem $path)) {
    If($file.LastWriteTime -lt $date.date)
    {
    #Test to see if the file already exists in the destination. If not => Move/Copy/Action you want :D
    if (!(Test-path (join-path $destination $file.name)))
    {
    Copy-Item -Path $file.fullname -Destination $destination
    }
    }
    }


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments