I’ve created a Function called Modify-Textfile that allows one to add or remove a line from a file based on complete or partial match. It also handles removing a single or multiple lines.
Is this something that might be useful to others? Are there any suggestions for improvements?
#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 Action Sets which action the function will perform. Options: AddLine, RemoveLine .PARAMETER Text Text to either Add to end of file or Search for and remove the first line that contains it. .PARAMETER Partial If Action is RemoveLine. If True, searches for part of the line to remove, If False, searches for entire line. Default $false .PARAMETER Multiple If Action is RemoveLine. 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' -Action AddLine -Text 'This is some text to append' .EXAMPLE Modify-TextFile -Path "$envWinDir\System32\TextFile.log" -Action 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)] [Validateset('AddLine','RemoveLine')] [string]$Action, [Parameter(Mandatory=$true)] [ValidateNotNullorEmpty()] [string]$Text, [Parameter(Mandatory=$false)] [ValidateNotNullorEmpty()] [boolean]$Multiple = $false, [Parameter(Mandatory=$false)] [ValidateNotNullorEmpty()] [boolean]$Partial = $false ) Begin { ## Get the name of this function and write header [string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -CmdletBoundParameters $PSBoundParameters -Header } Process { Try { # Check to see if text file exists If (Test-Path -Path $Path -PathType Leaf) { If ($Action -eq 'AddLine') { Write-Log -Message "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 "<code>r</code>n$Text" Add-Content -Path $Path -Value "$Text" } ElseIf ($Action -eq 'RemoveLine') { Write-Log -Message "Remove text ""[$text]"" from [$path]..." -Source {$CmdletName} # 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 Get-Content $Path | Where-Object {$_ -notmatch $Text} | Set-Content $TempPath } ElseIf ($Partial -eq $false) { # Remove multiple lines with complete string match Get-Content $Path | Where-Object {$_ -notlike $Text} | Set-Content $TempPath } } ElseIf ($Multiple -eq $false) { # Remove single line from text file If ($Partial -eq $true) { # Remove single line with partial string match $intLine = (Get-Content $Path | Where-Object {$_ -match $Text} | Select-Object -First 1).ReadCount Write-Host $intLine } ElseIf ($Partial -eq $false) { # Remove single line with complete string match $intLine = (Get-Content $Path | Where-Object {$_ -like $Text} | Select-Object -First 1).ReadCount Write-Host $intLine } If ($intLine -gt 0) { $fileContents = Get-Content $Path $fileContents -replace $fileContents[$intLine - 1],"" | Set-Content $TempPath } Else { # No matchine line Write-Log -Message "[$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 -Message "File [$Path] does not exist..." -Source ${CmdletName} } } Catch { Write-Log -Message "Failed to modify the text file [$path]. ` n$(Resolve-Error)" -Severity 3 -Source ${CmdletName} } } End { Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer } } #endregion