This function Adds a line to the end of a text file or Removes the first matching line or all matching lines from a text file either with a partial or complete match.
I’m using Modify-TextFile because I can’t find a better verb. Update doesn’t seem to fit since this is not, necessarily about getting an up-to-date version of anything.
#region Function Modify-TextFile
Function Modify-TextFile {
<#
.SYNOPSIS
Adds a line or removes a line from a text file.
.DESCRIPTION
Add a line to the end of a text file, or finds and removes a line from a text file.
.PARAMETER Path
Path to the text file
.PARAMETER AddLine
Switch to Add a line to a textfile
.PARAMETER RemoveLine
Switch to Remove a line/lines from a text file
.PARAMETER Text
Text to either Add to end of file or Search for and remove the first line that contains it.
.PARAMETER Partial
If RemoveLine switch is present. If True, searches for part of the line to remove, If False, searches for entire line. Default $false
.PARAMETER Multiple
If RemoveLine switch is present. If True, will remove all lines with Text. If False, removes only first matched line. Default $false
.EXAMPLE
Modify-TextFile -Path 'C:\Temp\TestFile.txt' -AddLine -Text 'This is some text to append'
.EXAMPLE
Modify-TextFile -Path "$envWinDir\System32\TextFile.log" -RemoveLine -Text 'Installation' -Multiple $true
.NOTES
Created by tv45057 for the TVDSB Deployment team
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,HelpMessage='Please enter the complete path to the text file')]
[ValidateNotNullorEmpty()]
[string]$Path,
[Parameter(Mandatory=$true,ParameterSetName='Add')]
[switch]$AddLine,
[Parameter(Mandatory=$true,ParameterSetName='Remove')]
[switch]$RemoveLine,
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[string]$Text,
[Parameter(Mandatory=$false,ParameterSetName='Remove')]
[ValidateNotNullorEmpty()]
[boolean]$Multiple = $false,
[Parameter(Mandatory=$false,ParameterSetName='Remove')]
[ValidateNotNullorEmpty()]
[boolean]$Partial = $false
)
# Check to see if text file exists
If (Test-Path -Path $Path -PathType Leaf) {
If ($AddLine) {
Write-Log -Text "Append text ""$text"" to [$path]..." #-Source {$CmdletName}
# Begin process of adding a line to the end of the file. We're assuming a Windows-formatted file right now
Add-Content -Path $Path -Value "$Text"
}
ElseIf ($RemoveLine) {
Write-Log -Text "Remove text ""[$text]"" from [$path]..."
# Begin the process of finding and removing a line/lines from the file
$TempPath = $Path + '.temp'
# Make backup of text file - remove current backup if it exists
$BackupPath = $Path + '.backup'
If (Test-Path -Path $BackupPath -PathType Leaf) {
Remove-File -Path $BackupPath
}
Copy-File -Path $Path -Destination $BackupPath
If ($Multiple -eq $true) {
# Remove lines with multiple instances of text from file
If ($Partial -eq $true) {
# Remove multiple lines with instances of partial string
$Contents = Get-Content $Path | Where-Object {$_ -notmatch $Text}
If ($Contents) {
Set-Content $TempPath
}
Else {
echo $null >> $TempPath
}
}
ElseIf ($Partial -eq $false) {
# Remove multiple lines with complete string match
$Contents = Get-Content $Path | Where-Object {$_ -notlike $Text}
If ($Contents) {
Set-Content $TempPath
}
Else {
echo $null >> $TempPath
}
}
}
ElseIf ($Multiple -eq $false) {
# Remove single line from text file
If ($Partial -eq $true) {
# Get index of first matching line with partial string match
$intLine = (Get-Content $Path | Where-Object {$_ -match $Text} | Select-Object -First 1).ReadCount
}
ElseIf ($Partial -eq $false) {
# Get index of first matching line with complete string match
Write-Host 'multiple-false partial-false'
$intLine = (Get-Content $Path | Where-Object {$_ -like $Text} | Select-Object -First 1).ReadCount
}
If ($intLine -gt 0) {
Write-Log -Text "Replacing [$Text] in line [$intLine]"
$fileContents = Get-Content $Path
$fileContents.Item($intLine-1) = "" # modify the array item found above - replace with empty string
$fileContents | Set-Content $TempPath
}
Else {
# No matchine line
Write-Log -Text "[$text] not found in [$path]"
}
}
# Remove original File and rename Temp file to match
If (Test-Path -Path $TempPath -PathType Leaf) {
Remove-File -Path $Path
Rename-Item -Path $TempPath -NewName $Path
}
}
}
Else {
# There is no such file - abort function
Write-Log -Text "File [$Path] does not exist..."
}
}
#endregion