I'm performing some pretty straightforward checksums on our linux boxes, but I now need to recreate something similar for our windows users. To give me a single checksum, I just run:
md5sum *.txt | awk '{ print $1 }' | md5sum
I'm struggling to recreate this in Windows, either with a batch file or Powershell. The closest I've got is:
Get-ChildItem $path -Filter *.txt |
Foreach-Object {
$hash = Get-FileHash -Algorithm MD5 -Path ($path + "\" + $_) | Select -ExpandProperty "Hash"
$hash = $hash.tolower() #Get-FileHash returns checksums in uppercase, linux in lower case (!)
Write-host $hash
}
This will print the same checksum results for each file to the console as the linux command, but piping that back to Get-FileHash to get a single output that matches the linux equivalent is eluding me. Writing to a file gets me stuck with carriage return differences
Streaming as a string back to Get-FileHash doesn't return the same checksum:
$String = Get-FileHash -Algorithm MD5 -Path (Get-ChildItem -path $files -Recurse) | Select -ExpandProperty "Hash"
$stringAsStream = [System.IO.MemoryStream]::new()
$writer = [System.IO.StreamWriter]::new($stringAsStream)
$writer.write($stringAsStream)
Get-FileHash -Algorithm MD5 -InputStream $stringAsStream
Am I over-engineering this? I'm sure this shouldn't be this complicated! TIA
The devil is in the details:
Get-FileHash
returns checksums in uppercase while Linuxmd5sum
in lower case (!);*.txt
is not case sensitive in PowerShell while in Linux depends on the optionnocaseglob
. If set (shopt -s nocaseglob
) then Bash matches filenames in a case-insensitive fashion when performing filename expansion. Otherwise (shopt -u nocaseglob
), filename matching is case-sensitive;Get-ChildItem
output is ordered according to Unicode collation algorithm while in Linux*.txt
filter is expanded in order ofLC_COLLATE
category (LC_COLLATE="C.UTF-8"
on my system).In the following (partially commented) script, three
# Test
blocks demonstrate my debugging steps to the final solution:Output (tested on 278 files):
.\SO\69181414.ps1