Answered by:
move older files(for example more than 30 days) to another path with directory by ps

Question
-
Hello, Expert,
I make a powershell to move older files to a path. I like to how to keep original directory?
The below script I am using. how to add new-item cmdlet to achieve that task?
+++++
$Sourcefolder= "G:\2. Common Procedures & Processes"
$Targetfolder= "G:\test\New folder"
Get-ChildItem -Path $Sourcefolder -Recurse|
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-180)
}| move-Item -Destination $Targetfolder++++
brgds
LiuWei
Answers
-
-
Hi LiuWei,
You can use
$Sourcefolder = "C:\temp"
$Targetfolder= "C:\temp2\New folder"
Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinueAll Files ... and directories are copied to the destination Path. The Tree is maintained.
When you run, if files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.
Olivier
- Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
-
Hi LiuWei,
You can use
$Sourcefolder = "C:\temp"
$Targetfolder= "C:\temp2\New folder"
Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinueAll Files ... and directories are copied to the destination Path. The Tree is maintained.
When you run, if files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.
Olivier
How does this copy only files older than 30 days? Perhaps you should read the whole thread before guessing at an answer.
\_(ツ)_/
- Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
All replies
-
-
Hi LiuWei,
You can use
$Sourcefolder = "C:\temp"
$Targetfolder= "C:\temp2\New folder"
Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinueAll Files ... and directories are copied to the destination Path. The Tree is maintained.
When you run, if files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.
Olivier
- Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
-
Hi LiuWei,
You can use
$Sourcefolder = "C:\temp"
$Targetfolder= "C:\temp2\New folder"
Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinueAll Files ... and directories are copied to the destination Path. The Tree is maintained.
When you run, if files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.
Olivier
How does this copy only files older than 30 days? Perhaps you should read the whole thread before guessing at an answer.
\_(ツ)_/
- Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM