Suggest Modify-Textfile function

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 {
    Adds a line or removes a line from a text file.
    Add a line to the end of a text file, or finds and removes a line from a text file.
    Path to the text file
    Sets which action the function will perform. Options: AddLine, RemoveLine
    Text to either Add to end of file or Search for and remove the first line that contains it.
    If Action is RemoveLine. If True, searches for part of the line to remove, If False, searches for entire line. Default $false
    If Action is RemoveLine. If True, will remove all lines with Text. If False, removes only first matched line. Default $false
    Modify-TextFile -Path 'C:\Temp\TestFile.txt' -Action AddLine -Text 'This is some text to append'
    Modify-TextFile -Path "$envWinDir\System32\TextFile.log" -Action RemoveLine -Text 'Installation' -Multiple $true
    Created by tv45057 for the TVDSB Deployment team
    Param (
        [Parameter(Mandatory=$true,HelpMessage='Please enter the complete path to the text file')]
        [boolean]$Multiple = $false,
        [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

I’m not sure how to get everything after the ‘newline’ in line 118 to stay in the ‘code’ section.

Thanks for sharing. I edited the post for you to make all of the code syntax highlighted. You can use the the “crayon” button in the text box to bring up a window that allows you to paste code which will then all be included as syntax highlighted coded.

Also, this function you’re sharing with the community should really be posted under the “Toolkit Extension” forum.

Now for suggestions about the code.

  1. Instead of naming the function with the “Modify” verb, I would use one of the approved verbs for naming functions (probably should be “Update” in this case). Microsoft has the list here and a description of each:

  2. For the catch block in your Try/Catch to actually catch any errors, you will have to add “-ErrorAction ‘Stop’” to the end of every cmdlet you call. Otherwise, the default PowerShell behavior is to display the error and continue.

Other then that, I think it looks great.

RE: suggestion #2, this should be helpful for some: