Wednesday 22 January 2014

SHA1 Powershell Script

[Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
$sha1 = new-Object System.Security.Cryptography.SHA1Managed

Function SHA1 {
    Param([string] $fn)

    $obj = New-Object System.Object
    $obj | Add-Member -type NoteProperty -name Name -value $fn

    $v = ""
    if (Test-Path $fn) {
        $a = Get-Item $fn
        if ($a.Attributes -eq 'Directory') { $v = "Directory" }
        else {
            try {
                $file = [System.IO.File]::Open($a.FullName, "open", "read")
                $sha1.ComputeHash($file) | %{ $v += $_.ToString("x2") }
                $file.Dispose()
            }
            catch { $v = "failed to open" }
        }
    }
    else { $v = "invalid path" }
    $obj | Add-Member -type NoteProperty -name SHA1 -value $V
    $obj
}
$TAB = @()
$args | %{
    try {
    Get-ChildItem -r $_ | %{ $TAB += SHA1 $_.FullName }
    }
    catch { }
}
$TAB | Format-Table -autosize -property SHA1,Name