Eric Bergman-Terrell's Blog

PowerShell Tip - Replace Text in Files that Might Have Square Brackets in Their File Names
June 18, 2015
# The -LiteralPath options are required if the filenames contain square brackets []

$items = Get-ChildItem -literalpath "{folder path}" -recurse
# enumerate the items array
foreach ($item in $items)
{
	# if the item is a directory, then process it.
	if ($item.Attributes -ne "Directory")
	{  
		(Get-Content -LiteralPath $item.FullName ) |
		Foreach-Object { $_ -replace '{old text}', '{new text}' } |
		Set-Content -LiteralPath $item.FullName
	}
}
Keywords: PowerShell, Filenames, Square Brackets, []

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015