David Tolo Technology Manager
asked on
powershell script that deletes older file in a directory running as a service
I need a Windows PowerShell script that will delete all the files that are over 2 weeks old in a directory and all subdirectory without deleting the subdirectories.
I then need to convert it to a service so that it is running in the background.
I then need to convert it to a service so that it is running in the background.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok.. I think that I almost understand it..
My only concern is that I will store the script in S:\Scripts
The files I want removed are in
S:\Backups
I am adding this as a scheduled tasks or as a service
Where can I add the logic for the script to run in s:\Backups\
My only concern is that I will store the script in S:\Scripts
The files I want removed are in
S:\Backups
I am adding this as a scheduled tasks or as a service
Where can I add the logic for the script to run in s:\Backups\
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
whould this work?
param($ParentDirectory)
$ParentDirectory = "S:\PhoneBackup\"
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse
foreach ($Item in $Items){
if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14)) ) {
try {
Remove-Item $Item.VersionInfo.FileName -Force
}
catch {
"Couldn't remove $($Item) - Error Message: $($_)" | Out-File s:\PhoneBackup\FailedLog.t xt -Append
Write-Error $_
}
}
}
param($ParentDirectory)
$ParentDirectory = "S:\PhoneBackup\"
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse
foreach ($Item in $Items){
if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14))
try {
Remove-Item $Item.VersionInfo.FileName
}
catch {
"Couldn't remove $($Item) - Error Message: $($_)" | Out-File s:\PhoneBackup\FailedLog.t
Write-Error $_
}
}
}
ASKER
I think we posted at the same time :-)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use this :)
param($ParentDirectory="S:\Backups")
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse
foreach ($Item in $Items){
if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14))) {
try {
Remove-Item $Item.VersionInfo.FileName -Force
}
catch {
Write-Host "Couldn't remove $Item"
Write-Error $_
}
}
}
ASKER
Thank you very much!!!
Happy to help! I want add that you should utilize my last snippet in your script since it sets the -ParentDirectory parameter automatically
ASKER
yes.. I ended up with
param($ParentDirectory="S: \PhoneBack up")
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse
foreach ($Item in $Items){
if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14)) ) {
try {
Remove-Item $Item.VersionInfo.FileName -Force
}
catch {
"Couldn't remove $($Item) - Error Message: $($_)" | Out-File s:\PhoneBackup\FailedLog.t xt -Append
Write-Error $_
}
}
}
param($ParentDirectory="S:
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse
foreach ($Item in $Items){
if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14))
try {
Remove-Item $Item.VersionInfo.FileName
}
catch {
"Couldn't remove $($Item) - Error Message: $($_)" | Out-File s:\PhoneBackup\FailedLog.t
Write-Error $_
}
}
}
ASKER
Where is the Directory specified?
I see that you have Write-Host "Couldn't remove $Item"
Where does this write too? Could this be set to write to a text file in the same folder?
If it is a scheduled task it would require no user intervention.
Also won't recurse make it delete the subfolders?