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
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021
Convert a Windows 10 Notebook into a High-Capacity Photo FrameApril 3, 2021