0

I am trying to use Get-ChildItem to produce a list of files, which I then want to delete, and then run the script again every five minutes via Task Scheduler, i.e. a fairly basic folder-watch task.

However it seems that the list of files is getting cached, so that newly created files are not 'seen' by the script...

Get-ChildItem –Path "\\server\share$\my\path" | where {($_ -like "_minidump_default_pid*.dmp")} | Remove-Item -Force | Out-File D:\MiniDump_Delete_Output.txt –Append

I then write a single char to the output file, so I can see that the task runs as expected.

MikeB
  • 580
  • 3
  • 18
  • has nothing to do with powershell, check the SMB server settings - u are right there is a cache, settings depend on OS version – Toni Sep 05 '22 at 16:37
  • 1
    The `| Out-File` part is redundant (`Remove-Item` doesn't output anything), and you probably want to be specific about which property you're testing (eg. `Where-Object { $_.Name -like "_minidump_default_pid*.dmp"}`) - but other than that your code looks fine. Did you inspect the task scheduler history to verify the script actually ran successfully the second time? – Mathias R. Jessen Sep 05 '22 at 16:38
  • @MathiasR.Jessen Yes, the task definitely runs - I write a char/line to that same output file every run, and that shows up. This was meant to be a two-minute clone of someone else's script - am now doubting whether the original works as intended! – MikeB Sep 05 '22 at 16:45
  • @Toni If I run a cmd prompt, or run the command via the PS editor, then it shows the new files. The sched task DID remove my first test file, but none that I added afterwards. – MikeB Sep 05 '22 at 16:47
  • "Remove-Item doesn't output anything" - yes it does, with the "-WhatIf" option, which I used at first. – MikeB Sep 05 '22 at 16:49
  • 1
    `-WhatIf` doesn't write to the success stream, as Mathias mentioned, `Out-File` isn't needed since `Remove-Item` doesn't produce anything as it consumes the objects beforehand. – Abraham Zinala Sep 05 '22 at 17:29
  • May be this can help : https://stackoverflow.com/a/9935126 – Frank Monroe May 04 '23 at 23:13

0 Answers0