PowerShell remove character text from all files in a directory SED powershell operation

Powershell script to remove text from each file in a directory.
*In Linux-Unix SED works very well for this.

POWERSHELL “Sed like”

Using variables to recurse a directory cheating using set-content to work like
regular expression

#remove a character from all files in a directory -=
 #REMOVE
#var old is text to remove
 $old = ','
#var $new = the text you want to replace ( I am replacing text with
#      a blank space)
$new = ''
#change the Get-ChildItem to a folder or your choice
Get-ChildItem C:\scripts\files\ -Recurse | Where {$_ -IS [IO.FileInfo]} |

% {

(Get-Content $_.FullName) -replace $old,$new | Set-Content $_.FullName
 Write-Host "----- WORKING   ----------"
 Write-Host "Processed: " + $_.FullName

}
 #REMOVE DONE

—– WORKING ———-
Processed: + c:\scripts\files\.txt
Processed: + c:\scripts\files\file1.txt
Processed: + c:\scripts\files\txt-159_2-5.txt
Processed: + c:\scripts\files\extf\ile0_0_140-159_6-10.txt
Processed: + c:\scripts\files\extftxt60-179_11-15.txt
Processed: + c:\scripts\files\extftxt60-179_2-5.txt
Processed: + c:\scripts\files\extftxt60-179_6-10.txt
Processed: + c:\scripts\files\txt122-188_11-15.txt
Processed: + c:\scripts\files\txt122-188_2-5.txt
Processed: + c:\scripts\files\txt122-188_6-10.txt
Processed: + c:\scripts\files\txt120-139_11-15.txt
Processed: + c:\scripts\files\txt120-139_2-5.txt
Processed: + c:\scripts\files\txt120-139_6-10.txt
Processed: + c:\scripts\files\txt140-159_11-15.txt
Processed: + c:\scripts\files\txt140-159_2-5.txt
Processed: + c:\scripts\files\txt140-159_6-10.txt
Processed: + c:\scripts\files\txt160-179_11-15.txt
Processed: + c:\scripts\files\txt160-179_2-5.txt
Processed: + c:\scripts\files\txt160-179_6-10.txt

share with friends

Share to Google Buzz
Share to Google Plus
Share to LiveJournal
Share to Odnoklassniki
No comments yet.

Leave a Reply