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
Related
I'm pretty new to powershell.
I need to learn it though.
I started with a simple script that deletes certain file extensions in different paths, if they are older than 10 days.
$DeleteDate = (Get-Date).AddDays(-10)
$path1 = "\\path1\*.tibx*"
$path2 = "\\path2\*Backup Set*"
$path3 = "\\path3\*.tibx*"
Get-ChildItem $path1 | Where {$_.LastWriteTime -lt "$DeleteDate"} | Remove-Item
Get-ChildItem $path2 -Recurse | Where {$_.LastWriteTime -lt "$DeleteDate"} | Remove-Item
Get-ChildItem $path3 | Where {_.LastWriteTime -lt "$DeleteDate"} | Remove-Item
This works so far. Now what I would also need is, that it is logging, what and when it was deleted.
The only problem about this is, that Remove-Item doesnt seem to have output. Not even with "verbose".
I thought about putting the Items in an array, then write the array before and after the delete in a log file.
But this seems kinda complex for a task this simple.
Do you have any Ideas that could help me?
Insert ForEach-Object in the pipeline(s) just before Remove-Item, then log it there:
Get-ChildItem $path1 | Where {$_.LastWriteTime -lt "$DeleteDate"} | ForEach-Object {
Write-Verbose "Deleting '$($_.FullName)' at [$(Get-Date -Format o)]"
$_
} | Remove-Item
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
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.
I want to delete all types of Files from below folder structure which is older than 30 Days. So I used below PowerShell Script. But it is not deleting any file.
PowerShell Script:
Get-ChildItem –Path "C:\Users\VJ\Documents\MainDir" –Recurse | Where-Object { $_.CreationTime –lt (Get-Date).AddDays(-30) } | Remove-Item
Folder Structure:
MainDir
|
SubDir1 -> Folder1 -> Folder2 -> Zip/CSV files
SubDir2 -> Folder1->.txt files
|
Folder2 -> .txt files
End Result should be all types of Files deleted from all folders - subfolders of MainDir.
I think you are confusing the properties CreationTime and LastWriteTime.
If you've just copied (old) files to one of the directories in the structure, the CreationTime is the date and time the file was copied there, which can be today. The LastWriteTime property however shows the date and time the file was last written to.
Try:
$refDate = (Get-Date).AddDays(-30)
Get-ChildItem -Path "C:\Users\VJ\Documents\MainDir" -Recurse -File |
Where-Object { $_.LastWriteTime -lt $refDate } |
Remove-Item -Force
If you're on PowerShell version less than 3.0, use:
Get-ChildItem -Path "C:\Users\VJ\Documents\MainDir" -Recurse |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $refDate } |
Remove-Item -Force
Also, check and retype your dashes, because they may LOOK like normal hyphens, in fact they are EN Dashes (Hex value 2013; Decimal 8211)
Hope that helps
Try this:
Get-ChildItem -Path "C:\Users\VJ\Documents\MainDir" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime –lt (Get-Date).AddDays(-30) } | Remove-Item -Force
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.