Remove content from files with Powershell

I love Windows PowerShell. It’s the missing shell/scripting environment that could never be properly satisfied with just DOS commands and batch files. I know it’s been out for a while now, but I’ve only recently started getting on top of it, and it’s been fantastic so far.

Quite often I find myself needing to do little maintenance jobs involving iterating through files and tranforming them — C# source code, SQL scripts, my MP3 collection etc. Powershell is the perfect tool for these sorts of tasks. Here’s a very simple (and probably not that efficient) snippet I wrote last week to strip out SQL Server Management Studio’s useless ‘descriptive headers’ comments from a nested hierarchy of 2,000 generated SQL scripts:

/****** Object:  Table [dbo].[hibernate_unique_key]    Script Date: 04/12/2009 11:35:29 ******/
CREATE TABLE [dbo].[hibernate_unique_key](
	[next_hi] [int] NULL

dir -recurse -filter *.sql $src | foreach ($_) {
	$file = $_.fullname
	echo $file
	(get-content $file) | where {$_ -notmatch "^s?/****** Object:.*$" } | out-file $file

How easy was that? I definitely need to learn more of this stuff!

One thought on “Remove content from files with Powershell

  1. Watch out for unintended variable expansion when you use double qoutes for regexes. You are OK in this case since the $ is the last char, but in general, unless you need variable expansion, I would recommend using single quotes for regexes. Also watch out for echo since it adds to the output and in this case will get written to your SQL file. If that isn’t what you intended, use Write-Host to display to the console which file is currently being processed. Or better yet, use Write-Progress.

Comments are closed.