# Define function to check if the first 6 characters of a file match a provided argument function Check-FirstSixCharacters { param ( [Parameter(Mandatory=$true)] [string]$FilePath, [Parameter(Mandatory=$true)] [string]$CompareString ) try { # Open the file and read the first 6 characters $stream = New-Object System.IO.FileStream($FilePath, [System.IO.FileMode]::Open) $reader = New-Object System.IO.StreamReader($stream, [System.Text.Encoding]::ASCII) $charArray = New-Object char[](6) $reader.Read($charArray, 0, 6) | Out-Null # Compare the first 6 characters to the provided string and return true if they match if ([string]::new($charArray) -eq $CompareString) { return $true } else { return $false } } catch { Write-Error "Error reading file: $FilePath" return $false } finally { # Close the stream and reader $reader.Close() $stream.Close() } } # Define function to get all files in a folder and its subfolders and apply the Check-FirstSixCharacters function to each file function Get-FilesWithFirstSixCharacters { param ( [Parameter(Mandatory=$true)] [string]$FolderPath, [Parameter(Mandatory=$true)] [string]$CompareString ) try { # Get all files in the folder and its subfolders $files = Get-ChildItem -Path $FolderPath -Recurse -File # Loop through each file and apply the Check-FirstSixCharacters function $results = foreach ($file in $files) { $match = Check-FirstSixCharacters -FilePath $file.FullName -CompareString $CompareString [PSCustomObject]@{ "FilePath" = $file.FullName "Match" = $match } } # Output the results as a table $results | Format-Table -AutoSize } catch { Write-Error "Error getting files: $FolderPath" } } # Usage example Get-FilesWithFirstSixCharacters -FolderPath "C:\path\to\your\folder" -CompareString "Hello "