Subscribe

SQLServer-DBA.com Links

Subscribe to newsletter

Dba_db2_button

Powered by TypePad
SQL Server - Search and Delete files from a text based list with Powershell

02 January,2013 by Jack Vamvas

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
}

 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: Jack Vamvas (http://www.sqlserver-dba.com)

******** *******

Subscribe to SQLServer-DBA.com

Latest posts delivered to you daily

Delivered by FeedBurner

Verify your Comment

Previewing your Comment

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

Working...
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.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer