Functions/Add-LineToFile.ps1
<#
.SYNOPSIS This function adds a line to either the front or the back of a text file. #> function Add-LineToFile { [CmdletBinding(PositionalBinding=$true, DefaultParameterSetName="Front")] param ( # The path of the text file. [Parameter(Mandatory=$true, Position=0)] [ValidateNotNullOrEmpty()] [String]$path, # The line to add to the file. [Parameter(Mandatory=$true, Position=1)] [AllowNull()] [String]$line, # Select to add the line to the front of the file. [Parameter(Mandatory=$false, ParameterSetName="Front")] [Switch]$front = [Switch]::Present, # Select to add the line to the back of the file. [Parameter(Mandatory=$true, ParameterSetName="Back")] [Switch]$back ) # Retrieve the current contents of the file $currentFileContents = Get-Content -Path $path -Raw # Add the line to the file if ($PSCmdlet.ParameterSetName -eq "Front") { $newFileContents = "$($line)`r`n$($currentFileContents)" } else { $newFileContents = "$($currentFileContents)`r`n$($line)" } # Set the new file contents $newFileContents | Set-Content -Path $path -NoNewline } |