Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 18, 2014 5:30 AM
Hi,
Is it possible to help me with a powershell script that delete files under a folder and subfolders that have the following attributes
1. have the name (default.*) and (index.*)
2. created on a specific date say 1-9-2014
3. created between specific time say between 10:00 - 16:00
Thanks in advance!
All replies (9)
Monday, September 22, 2014 11:20 AM ✅Answered
Hi,
Try this:
$path=$env:userprofile
$startdate=[datetime]::ParseExact("01-09-2014 10:00","dd-MM-yyyy HH:mm",$null)
$enddate=[datetime]::ParseExact("01-09-2014 16:00","dd-MM-yyyy HH:mm",$null)
#ps3 version
Get-ChildItem -File -Path $path -Include "default.*","index.*" -Recurse | where {$_.CreationTime -gt $startdate -and $_.CreationTime -lt $enddate}| Remove-Item -WhatIf
#ps2 version
Get-ChildItem -Path $path -Include "default.*","index.*" -Recurse | where {$_.CreationTime -gt $startdate -and $_.CreationTime -lt $enddate -and !$_.PSIsContainer} | Remove-Item -WhatIf
Good luck)
Thursday, September 18, 2014 8:03 AM
Try following the following article:
Thursday, September 18, 2014 12:29 PM
Hi RBoyde,
Thank you for you answer. i already read this article and this does not help since its only sort files and it only works on 1 folder levels and does not scan Child level.
i have one Parent folder and inside it hunders of Child folders. inside each Child folder there are hunders of folders.
i need to delete files with specific name that are created on specific date for all files located under Parent folder.
Thursday, September 18, 2014 1:07 PM
Hi,
Get-ChildItem has a -Recurse parameter you can use to include subfolders. As for testing on the creation date/time, the files have a CreationTime property you can use to filter on.
Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)
Thursday, September 18, 2014 1:22 PM
This is what i use to delete all files (not folders) in a particular folder ($BackupPath) that are more than 10 days old. Maybe you can use some of it.
get-childitem "$BackupPath\*.*" | where {$_.lastwritetime -lt (get-date).adddays(-10) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force }
Friday, September 19, 2014 5:22 AM
Have a play with this.
Get-ChildItem -Recurse c:\temp\*.* | Where-Object {$_.CreationTime -gt (get-date "01-09-2014 10:00AM") -and $_.
CreationTime -lt (get-date "01-09-2014 14:00PM")} | Where-Object {$_.name -match "default"} | Remove-Item
This will match any file with the default string in its name and remove it.
Cheers,
Martin
Blog: http://sustaslog.wordpress.com LinkedIn:
Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Monday, September 22, 2014 7:52 AM
Thanks all for your replay.
I tried Martin script as it seems that it was the nearest of my need.I changed the script parameters to meet my needs but it does not work. I dont recieve any errors either but no files has been deleted.
Is it have to do with that the server is Windows 2003. I followed this link http://it.curu.ca/how-to-install-powershell-on-windows-server-2003-and-enable-remote-powershell-management/
Monday, September 22, 2014 11:10 PM
break out the components of the script.
IE does this match
Get-ChildItem -Recurse c:\temp\*.*or Get-ChildItem -Recurse c:\temp\*.* | Where-Object {$_.CreationTime -gt (get-date "01-09-2014 10:00AM") -and $_.
CreationTime -lt (get-date "01-09-2014 14:00PM")}Get-ChildItem -Recurse c:\temp\*.* | Where-Object {$_.CreationTime -gt (get-date "01-09-2014 10:00AM") -and $_.
CreationTime -lt (get-date "01-09-2014 14:00PM")} | Where-Object {$_.name -match "default"}
Might not be working as it might not be matching.
Once you get it matching the remove-item will work.
Cheers,
Martin
Blog: http://sustaslog.wordpress.com LinkedIn:
Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Friday, October 17, 2014 6:54 AM
thanks all for you help.
Kolomiets unfortenattely the script didnt work but i used your idea of get-childitem -recurse to get a list of all the files and i added the Creation date as well as the file full path.
after that i used the full path with a script to delete all the files.
Thank you for you help!