02 January,2013 by Tom Collins
A regular systems administrator task is to find unauthorized files on a server and remove them.
For example, the files may be on a virus exclusion list or a list of files prohibited by the organisation.
It’s an easy task to complete with a Powershell script. The script example uses the : Get-Content & Get-ChildItem cmdlets to create the list of items.
The script uses the basic programming process of looping, repeating the command. The foraech statement loops through a series of values in a collection of items.
I’ve included the contents of the “c:\filelist.txt” - with two example files. Note:The script deletes the files automatically , so be careful in implementing.
--contents of filelist.txt ---file1.txt ---file2.txt $list = "" $list = Get-Content "C:\filelist.txt" $folder = Get-ChildItem -path "C:\testing\removeitem\" -Recurse -Include $list foreach ($i in $folder) { Remove-item $i.fullname "Deleted file:" + $i.fullname }
Powershell Get-ChildItem with examples - SQL Server DBA
SQL Server - How to write remove-item results to text with powershell out-file
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |