foreach($folder in (Get-ChildItem f:\backups)){ #Finds all forlders on f:\backups
#Counts the number of VBK files on the folder
$numFiles = (Get-ChildItem f:\backups\$folder -Recurse -Include "*.vbk").Count
#If the number of files > 1 then delete the files if not nothing to delete
If ($numFiles -gt 1)
{
Write-Host 'Deleting VBN file on ' $folder
Get-ChildItem f:\backups\$folder -Recurse -Include "*.vbm" | Remove-Item -Force
Write-Host 'VBN file succesfully deleted on ' $folder
Write-Host 'Deleting VBK file on ' $folder
Get-ChildItem f:\backups\$folder -Recurse -Include "*.vbk" | Sort-Object lastwritetime | Select-Object -first 1 | remove-item -force
Write-Host 'VBK file succesfully deleted on ' $folder
}
else {
Write-Host "Only one file so nothing was deleted"
}
}
#Coping files to the F Drive
Write-Host "coping files to F...."
#Finds all folders in e:\backups
foreach($folder in (Get-ChildItem e:\backups)){
#Copies the VBN file
Write-Host 'Coping VBN file to ' $folder
Get-ChildItem e:\backups\$folder -Recurse -Include "*.vbm" | Copy-Item -Destination F:\backups\$folder
Write-Host 'VBN file succesfully copied to ' $folder
#Copies the VBK file
Write-Host 'Coping VBK file to ' $folder
Get-ChildItem e:\backups\$folder -Recurse -Include "*.vbk" | sort-object lastwritetime | Select-Object -last 1 | Copy-Item -Destination F:\backups\$folder
Write-Host 'VBK file succesfully copied to ' $folder
}
Write-Host 'All Done '
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.