today i was trying to remove some junk folders and files in a specific folder. But i don’t want to remove all of file and folder , i want to keep few folders. Then i think that i should use the –Exclude switch with Remove-Item Cmdlet.
I tried to use –Exclude switch but somehow i failed to get it working because i don't know the exact patter used by –Exclude switch. After few minutes of testing i get it done and i thought i should share this tip with you.
- $Lenovo = "D:\P-Temp\Lenovo\*"
- Remove-Item -Recurse -Path $Lenovo -Exclude system,temp,updates.ser,"*.xml" -Verbose –Force
I want to Exclude folder name “System”,”temp” and file name “updates.ser” and all .XML Files
in -Exclude switch give the folder name which you don’t want to delete, no need to put the in a double quotes “” in folder name . You can provide multiple folder name separated by comma.
in pattern matching make sure you put the wild cards in double quotes “”.
Lets Test it !!
see we have System,Temp, few XML and one Updates.Ser file which we exclude to deleted. lets run the script now and see which file is getting deleted.
after running the script .
All folders are stay intact which we exclude in the Remove-Item cmdlet.
I hope that i helps someone .