Unable to delete old .bak files

Hello Forum,

 

Im using the following commands in powerGUI and it works intermittently and when used in SQL Agent is never works , this is the first step to delete old backups before creating the new ones.

I have tried both scripts but nothing works ..

 

1.Get-ChildItem $BakLoc -recurse -include *.bak |Remove-Item -force -recurse -ErrorAction SilentlyContinue

 

 2. foreach($file in $BakLoc)

   {

      $FilePath = $file.fullname

 IF((Test-Path $FilePath) -eq $True){remove-item -Literalpath $FilePath -ErrorAction SilentlyContinue}

   }

Parents
No Data
Reply Children
No Data