Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions Links


SQL Server DBA Jobs

How to Search and Delete files from a text based list with Powershell

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

$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

 Read More

Powershell Get-ChildItem with examples - SQL Server DBA

Powershell Scripts

SQL Server - How to write remove-item results to text with powershell out-file

Author: Tom Collins (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

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.


Post a comment on How to Search and Delete files from a text based list with Powershell | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer