通常为了保证我们从网上下载的文件的完整性和可靠性,我们把文件下载下来以后都会校验一下MD5值或SHA1值(例如验证[下载的Win10 ISO镜像]是否为原始文件),这一般都需要借助专门的MD5检验工具来完成。但其实使用Windows系统自带的Windows PowerShell运行命令即可进行文件MD5、SHA1值校验。方法如下:
在Win10开始按钮上点击右键,选择“Windows PowerShell(管理员)”打开“管理员: Windows PowerShell”窗口。
校验文件Hash值的命令格式如下:
> Get-FileHash 文件路径 -Algorithm 校验的Hash值类型| Format-List
PS: 如果需要校验的文件路径比较复杂,例如路径中包含空格、括号等特殊符号,则需要在路径前后加上英文双引号。
Windows PowerShell命令可以校验的Hash值类型包括:SHA1、SHA256、SHA384、SHA512、MACTripleDES、MD5、RIPEMD160,暂不支持校验CRC32值。
如果不带-Algorithm参数,也就是不指明验证的Hash值类型,那么默认验证的就是SHA256值。
下面MS酋长举个例子,以验证系统的记事本程序的Hash值为例,程序文件的路径为C:\Windows\notepad.exe。如果我们想要校验它的MD5值,则运行如下命令:
> Get-FileHash C:\Windows\notepad.exe -Algorithm MD5| Format-List
巧用Win10自带的PowerShell命令校验文件的Hash值(MD5、SHA1/256等)
如果想要校验它的SHA1值,则运行如下命令:
> Get-FileHash C:\Windows\notepad.exe -Algorithm SHA1| Format-List
如果想要校验SHA256值,则不需要带-Algorithm参数即可,命令如下:
> Get-FileHash C:\Windows\notepad.exe | Format-List
可以把这些命令添加到右键菜单中,使用更加方便。如图:
image.png
把下面的命令保存为Hash.reg文件,然后双击运行就行了:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\文件哈希校验]
"SubCommands"="MACTripleDES;MD5;RIPEMD160;SHA1;SHA256;SHA384;SHA512"
"MUIVerb"="文件哈希校验"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MACTripleDES]
@="MACTripleDES"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MACTripleDES\command]
@="PowerShell Get-FileHash -Algorithm MACTripleDES \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MD5]
@="MD5"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MD5\command]
@="PowerShell Get-FileHash -Algorithm MD5 \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\RIPEMD160]
@="RIPEMD160"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\RIPEMD160\command]
@="PowerShell Get-FileHash -Algorithm RIPEMD160 \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA1]
@="SHA1"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA1\command]
@="PowerShell Get-FileHash -Algorithm SHA1 \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA256]
@="SHA256"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA256\command]
@="PowerShell Get-FileHash -Algorithm SHA256 \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA384]
@="SHA384"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA384\command]
@="PowerShell Get-FileHash -Algorithm SHA384 \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA512]
@="SHA512"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA512\command]
@="PowerShell Get-FileHash -Algorithm SHA512 \"%1\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"