Created
August 9, 2015 23:39
-
-
Save adbertram/82dc89cb8a0f46d1da2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Find-InTextFile | |
{ | |
<# | |
.SYNOPSIS | |
Performs a find (or replace) on a string in a text file or files. | |
.EXAMPLE | |
PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water' -Replace 'wine' | |
Replaces all instances of the string 'water' into the string 'wine' in | |
'C:\MyFile.txt'. | |
.EXAMPLE | |
PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water' | |
Finds all instances of the string 'water' in the file 'C:\MyFile.txt'. | |
.PARAMETER FilePath | |
The file path of the text file you'd like to perform a find/replace on. | |
.PARAMETER Find | |
The string you'd like to replace. | |
.PARAMETER Replace | |
The string you'd like to replace your 'Find' string with. | |
.PARAMETER UseRegex | |
Use this switch parameter if you're finding strings using regex else the Find string will | |
be escaped from regex characters | |
.PARAMETER NewFilePath | |
If a new file with the replaced the string needs to be created instead of replacing | |
the contents of the existing file use this param to create a new file. | |
.PARAMETER Force | |
If the NewFilePath param is used using this param will overwrite any file that | |
exists in NewFilePath. | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'NewFile')] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateScript({ Test-Path -Path $_ -PathType 'Leaf' })] | |
[string[]]$FilePath, | |
[Parameter(Mandatory = $true)] | |
[string]$Find, | |
[Parameter()] | |
[string]$Replace, | |
[Parameter()] | |
[switch]$UseRegex, | |
[Parameter(ParameterSetName = 'NewFile')] | |
[ValidateScript({ Test-Path -Path ($_ | Split-Path -Parent) -PathType 'Container' })] | |
[string]$NewFilePath, | |
[Parameter(ParameterSetName = 'NewFile')] | |
[switch]$Force | |
) | |
begin | |
{ | |
$SystemTempFolderPath = Get-SystemTempFolderPath | |
if (!$UseRegex.IsPresent) | |
{ | |
$Find = [regex]::Escape($Find) | |
} | |
} | |
process | |
{ | |
try | |
{ | |
Write-Log -Message "$($MyInvocation.MyCommand) - BEGIN" | |
foreach ($File in $FilePath) | |
{ | |
if ($Replace) | |
{ | |
if ($NewFilePath) | |
{ | |
if ((Test-Path -Path $NewFilePath -PathType 'Leaf') -and $Force.IsPresent) | |
{ | |
Remove-Item -Path $NewFilePath -Force | |
(Get-Content $File) -replace $Find, $Replace | Add-Content -Path $NewFilePath -Force | |
} | |
elseif ((Test-Path -Path $NewFilePath -PathType 'Leaf') -and !$Force.IsPresent) | |
{ | |
Write-Warning "The file at '$NewFilePath' already exists and the -Force param was not used" | |
} | |
else | |
{ | |
(Get-Content $File) -replace $Find, $Replace | Add-Content -Path $NewFilePath -Force | |
} | |
} | |
else | |
{ | |
(Get-Content $File) -replace $Find, $Replace | Add-Content -Path "$File.tmp" -Force | |
Remove-Item -Path $File | |
Rename-Item -Path "$File.tmp" -NewName $File | |
} | |
} | |
else | |
{ | |
Select-String -Path $File -Pattern $Find | |
} | |
} | |
Write-Log -Message "$($MyInvocation.MyCommand) - END" | |
} | |
catch | |
{ | |
Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3' | |
Write-Log -Message "$($MyInvocation.MyCommand) - END" | |
$false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment