Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

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


Share:

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 on How to Search and Delete files from a text based list with Powershell


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