Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

powershell script for archiving files

Posted on 2009-04-15
1
Medium Priority
?
741 Views
Last Modified: 2012-06-27
Hello,

Can someone help me with a powershell script.
I need to do a recursive research on a folder and subfolder to find files that are older that 3 years (modified date) and then move these files to an other location.
I would like to respect the "tree" (folders and subfolders) and also delete the empty folders at the end.

Thank you for your help.
0
Comment
Question by:virginie8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 24157115

This should do it for you.

HTH

Chris

$SourceFolder = "C:\Source"
$DestinationFolder = "C:\Destination"
 
# Return all files in the structure which have not been written in the last 3 years
 
$Files = Get-ChildItem $SourceFolder -Recurse | ?{ 
  $_.PsIsContainer -eq $False -And `
  $_.LastWriteTime -lt ([DateTime]::Now).AddYears(-3) 
} | Select-Object Name, FullName, DirectoryName, LastWriteTime
 
# Move the files
 
$Files | %{
  $Destination = ($_.DirectoryName).Replace($SourceFolder, $DestinationFolder)
  If (!(Test-Path $Destination)) { [Void]New-Item -Path $Destination -Type Directory }
  (Get-Item $_.FullName).MoveTo("$Destination\$($_.Name)")
}
 
# Remove empty folders
 
$FoldersToDelete = Get-ChildItem $SourceFolder -Recurse | ?{ 
  $_.PsIsContainer -eq $True -And 
  (Get-ChildItem $_.FullName).Count -eq $Null 
}
$FoldersToDelete | %{ Remove-Item $_.FullName }

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question