TextFilesHandling/Join-ITIObjectsTxtFile.ps1

<#
.SYNOPSIS
    Joins all .txt files from a specified directory to a single .txt file
.DESCRIPTION
    Joins all the files in the specified directory including the files from all subdirectories of the specified directory
.EXAMPLE
    Join-ITIObjectsTxtFile -SourcePath ~/MyProject/Apps -Destination ~/MyProject/allObjects.txt
.NOTES
    The function includes all .txt files. It does not include any other text files like .md.
#>

function Join-ITIObjectsTxtFile {
    [CmdletBinding()]
    param (
        # Directory where the .txt objects are stored
        [string] $SourcePath = './Apps',
        # Path to the file where the joint objects will be saved
        [string] $Destination = './objects.txt'
    )
    if (SourcesInSubfolders($SourcePath)) {
        $objectFilesContent = JoinObjectsFromSubfolders($SourcePath)
    } else {
        $objectFilesContent = JoinObjectsFromFolder($SourcePath)
    }
    $objectFilesContent | Set-Content -Path $Destination -Encoding Oem
}

function SourcesInSubfolders($SourcePath) {
    $sourceFileList = Get-Item (Join-Path $SourcePath '*.txt')
    return ($null -eq $sourceFileList)
}

function JoinObjectsFromSubfolders($SourcePath) {
    $objects = @()
    $subfolders = Get-ChildItem -Path $SourcePath -Directory
    foreach($subfolder in $subfolders){
        $subfolderPath = Join-Path $SourcePath $subfolder
        $objects += JoinObjectsFromFolder($subfolderPath)
    }
    return $objects
}
function JoinObjectsFromFolder($SourcePath) {
    $objects = Get-Item -Path (Join-Path $SourcePath "*.txt") | Get-Content -Encoding Oem
    return $objects
}

Export-ModuleMember -Function Join-ITIObjectsTxtFile