-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompare-FileHash.ps1
More file actions
104 lines (87 loc) · 3.04 KB
/
Compare-FileHash.ps1
File metadata and controls
104 lines (87 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<#
.SYNOPSIS
Compare file hashes between two files or a file and a known hash to ensure they are identical.
.DESCRIPTION
This script calculates the hash of one or two files and compares them. It supports using a known hash for comparison. The script uses the Get-FileHash cmdlet with an algorithm such as SHA256.
.PARAMETER Path1
The file path for the first file to be compared.
.PARAMETER Path2
(Optional) The file path for the second file to be compared. Omit this parameter if using a known hash.
.PARAMETER KnownHash
(Optional) The known hash to compare the first file against. Use this instead of Path2.
.PARAMETER Algorithm
(Optional) The hashing algorithm to use (e.g., SHA256, SHA1). Default is SHA256.
.EXAMPLE
Compare-FileHash.ps1 -Path1 "C:\file1.txt" -Path2 "C:\file2.txt"
.EXAMPLE
Compare-FileHash.ps1 -Path1 "C:\file1.txt" -KnownHash "D7F37A6BC..."
.NOTES
Author: Tony Burrows
Date: 2024-08-30
Version: 1.1
#>
function Compare-FileHash {
param (
# Path of first file
[Parameter(
Mandatory = $true,
Position = 0,
ParameterSetName = 'CompareTwoFiles',
HelpMessage = 'Path of file 1'
)]
[Parameter(
Mandatory = $true,
Position = 0,
ParameterSetName = 'CompareFileToHash',
HelpMessage = 'Path of file 1'
)]
[string] $SourcePath,
# Path of second file
[Parameter(
Mandatory = $true,
Position = 1,
ParameterSetName = 'CompareTwoFiles',
HelpMessage = 'Path of file 2'
)]
[string] $ComparePath,
# Hash to compare
[Parameter(
Mandatory = $true,
Position = 1,
ParameterSetName = 'CompareFileToHash',
HelpMessage = 'Hash to compare'
)]
[string] $CompareHash,
# Record type
[Parameter(
Mandatory = $false,
Position = 2,
ParameterSetName = 'CompareTwoFiles',
HelpMessage = 'Algorithm type'
)]
[Parameter(
Mandatory = $false,
Position = 2,
ParameterSetName = 'CompareFileToHash',
HelpMessage = 'Algorithm type'
)]
[ValidateSet('MACTripleDES', 'MD5', 'RIPEMD160', 'SHA1', 'SHA256', 'SHA384', 'SHA512')]
[string] $Algorithm = 'SHA256'
)
switch ($PSCmdlet.ParameterSetName) {
'CompareTwoFiles' {
$Hash1 = $(Get-FileHash -Algorithm $Algorithm -Path "$SourcePath").Hash
$Hash2 = $(Get-FileHash -Algorithm $Algorithm -Path "$ComparePath").Hash
break
}
'CompareFileToHash' {
$Hash1 = $(Get-FileHash -Algorithm $Algorithm -Path "$SourcePath").Hash
$Hash2 = $CompareHash
}
}
if ($Hash1 -eq $Hash2) {
return $true
} else {
return $false
}
}