I got a ticket today requesting that I make a script to remove all of the files from a directory except for three files with a specific name and to have it run everyday. So I made a simple one liner in PowerShell.
Get-ChildItem -LiteralPath "\pathtodirectory" | Remove-Item -Exclude "filetosave1.xls", "filetosave2.xls", "filetosave3.xls" -Force
When I run the script from my computer I get the expected result. All files except for the three that I want to keep are deleted. My plan is to put the script on the file server and set a scheduled task to have it run every night. For some reason when I run the script from the file server it deletes all files in the directory and ignores my exclusions. Is the -Exclude parameter not available in older versions of PowerShell? Any help is appreciated.