Move Files older then 31 days to another drive - powershell

Function Move {
#Moves all files older than 31 days old from the Source folder to the Target
Get-Childitem -Path "E:\source" | Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-31)} |
ForEach {
Move-Item $_.FullName -destination "F:\target" -force -ErrorAction:SilentlyContinue
}
}
in the source directory are files that are older than 2-3 years, but when i run the script nothing moves to the target directory ?! whats wrong ?

I don't know if this makes much of a difference, but rather than $. it needs to be $_.
I tried this script and it works fine for me:
get-childitem -Path "E:\source" |
where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} |
move-item -destination "F:\target"
Notice you don't need a foreach loop because the objects will be "piped" into the move-item command

Also be aware of hidden files, try adding -Force to Get-ChildItem

Related

I want to maintain current date files

Delete all Files in C:\temp older than Current day(s)
$Path = "E:\Testing\Order\123456"
$CurrentDate = Get-Date
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $CurrentDate } | Remove-Item
I am trying to run this script but it is deleting every thing. It is not maintain current date files. Is there any modification please suggesting me sir.
Make sure you get the file extensions right if they have any.
Get-ChildItem -Path C:\folder1\data -Include * -Exclude text.1, folder1 -Recurse | foreach { $_.Delete()}
Edit to answer the comment:
So you want to delete all files and folders in C:\folder1 except of files text.1 and folder.1 in data, other and alpha? It means you cannot remove these 3 folders too so they have to be excluded.
Get-ChildItem -Path C:\folder1\ -Include * -Exclude text.1, folder.1, alpha, data, other -Recurse | foreach { $_.FullName}
try this (and dont stay into your dir when you try) :
Get-ChildItem "C:\folder1\data\*" -Recurse | where Name -notin ('text.1', 'folder.1') | Remove-Item -Force -Recurse

Powershell : How to delete specific format file (.xlsx only) older than X days

I am currently new at PowerShell, I want to delete .xlsx files older than X days using Powershell.
I tried below
Get-ChildItem D:\temp | ? { $_.PSIsContainer -and $_.LastWriteTime -lt $timeLimit } | Remove-Item -WhatIf
But above command deleted my all data (including C drive data)
Please suggest a modification or a new command. Thanks in Advance.
You do not appear to be filtering for xlsx files in your code. An xlsx file is not a folder so I have removed that condition from your filter.
$timelimit is not defined in the code shown but will need to be of the dateTime type to enable a comparison
Get-ChildItem d:\temp -filter "*.xlsx"| ? { $_.LastWriteTime -lt $timeLimit } | Remove-Item -WhatIf
This should do the trick:
$timeLimit = 60 # Days
Get-ChildItem -Path "D:\temp" -Recurse -Include *.xlsx | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-$timeLimit)} | Remove-Item -WhatIf
If this outputs the expected result, you can remove the -WhatIf switch and give it a try.

Powershell - Move files 1 folder deeper

I'm looking to move a large set of pdf files one folder deeper.
The current file structure is:
[Reference Code]\[file].pdf
and i'm looking to move the files to:
[Reference Code]\April 18\[file].pdf
if i recall correctly this could be done in linux with mv */*.pdf */April 18/*.pdf but a solution for windows seems to be a bit more complicated
$rootPath = "C:\"
$moveTo = "C:\April 18"
foreach ($pdfFile in (Get-ChildItem $rootPath | Where-Object {$_.Extension -eq ".pdf"}))
{
Move-Item -Path $pdfFile.FullName -Destination "$moveTo\$($pdfFile.Name)"
}
Like this?
One possibility:
$rootDir = "Reference Code"
Get-ChildItem -Path "$rootDir\*.pdf" -File |
ForEach-Object {
Move-Item $_.FullName -Destination "$rootDir\April 18\$($_.Name)"
}
Note that this will fail if folder April 18 doesn't exist.

Removing log files doesn't work

I have a PowerShell script which I plan to use to delete log files that are over a certain amount of days old
$path = "C:\users\example\desktop\test\*"
Get-ChildItem -Path $path -Include *txt | Where-Object {
$_.LastWriteTime -lt (Get-Date).AddDays(-30)
} | Remove-Item
Now the script works fine on test txt files that I have created, but it doesn't work on the log files. The files are just over 400 MB each, so is there a limit on the file size Remove-Item can handle? Or is there something I am missing?
Don't include a wildcard in your path.
GCI C:\Users\Example\Desktop\Test -Include *txt |
? { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
Remove-Item -Force

PowerShell format output

I'm trying to write a script that deletes all folders that are older than 60 days and create a logfile with the folder names in the directory where the folders have been deleted.
What I have now is:
Get-ChildItem -Directory -Path "\\share\dir1\dir2" |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } |
Remove-Item -Force -Recurse | Out-File Auto_Clean.log -Append -WhatIf
The output stays like this for ages:
What if: Performing the operation "Output to File" on target "C:\users\bgijbels\Downloads\Auto_Clean.log".
When I remove the part for Out-File it works fine. It seems like the Out-File part is trying to write the name of every file in the folder to the log, while I only need to have the folder name. I think that's why it takes so long, if at all it gets past the part of creating the logfile. Any ideas? Thank you for your help.
You are getting a list of all files because -Recurse switch enumerates contents of folders so it can be deleted prior to the root folder removal. Try this:
Get-ChildItem -Directory -Path "\\share\dir1\dir2" |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-60) } | % {
$folder = $_;
Remove-Item $folder.FullName -Force -Recurse | Out-Null
$folder.FullName } |
Out-File Auto_Clean.log -Append -WhatIf
Directory object is kept as $folder var and you effectively echo its full path after deletion. Obviously take -WhatIf off the end after you are happy with results.