PowerShell 4.0 新增计算文件hash值的命令:Get-FileHash,非常有用,从此我们计算文件hash再也不用下载什么软件。
不管是微软官方提供的还是第三方的,尤其第三方的谁知道是否植入了木马。
下面来看下通过 get-help get-filehash 获取到的帮助,用法很简单了:
PS C:\Users\Administrator> get-help get-filehash 名称 Get-FileHash 语法 Get-FileHash [-Path] <string[]> [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | RIPEMD 160}] [] Get-FileHash -LiteralPath <string[]> [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | R IPEMD160}] [] 别名 无 备注 Get-Help 无法在此计算机上找到此 cmdlet 的帮助文件。只能显示部分帮助。 -- 若要下载和安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。 PS C:\Users\Administrator>
比如我们要获取d盘下cn_win8.1.iso的sha1,我们可以打开powershell,输入如下命令:
Get-FileHash -Path d:\cn_win8.1.iso -Algorithm SHA1
敲回车就会看到cn_win8.1.iso的sha1值了,速度很快,如果文件比较大,稍微等一会就好了。
配合win8强大的复制文件地址功能,任何复杂路径下的文件我们都只需要复制粘贴就可以了,不用自己输入d:\cn_win8.1.iso。
win8如此强大,还是很多人不喜欢我很纳闷,这个技巧稍微提一下吧,方便读者:
我知道的有两种方法,还有其它方法没,没有深入研究,这两个足够用啦:
1.选中文件,点击资源管理器上方 Ribbon 界面中的复制路径即可。
2.选中文件,按住shift键,在选中的文件上点击鼠标右键,点击复制为路径即可。
之后,在powershell中点击鼠标右键,就会自动粘贴了,如果在cmd窗口中就要先点击鼠标右键,再选择粘贴才行。
如果不是powershell 4.0环境的话,可以将如下代码保存为powershell格式的ps1文件,然后通过命令行或者拖拽的方式都可以计算文件的hash值。如果懒得自己建文件,下面我也会提供百度盘的链接,大家可以下载,里面有readme,写的很详细,请先仔细阅读。
注:本脚本不是我写的,是“俺没喝过雄黄酒”在2010年9月6日创作的,我只是转载了。
#region define [Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null #write by 俺没喝过雄黄酒 #2010-9-6 #endregion define #region function function get-hash-window { #region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null #endregion #region Generated Form Objects [System.Windows.Forms.Application]::EnableVisualStyles() $sha1 = [System.Security.Cryptography.SHA1]::Create() $sha256 = [System.Security.Cryptography.SHA256]::Create() $sha512 = [System.Security.Cryptography.SHA512]::Create() $md5 = [System.Security.Cryptography.MD5]::Create() $form1 = New-Object System.Windows.Forms.Form $richTextBox1 = New-Object System.Windows.Forms.RichTextBox $button1 = New-Object System.Windows.Forms.Button $button2 = New-Object System.Windows.Forms.Button $button3 = New-Object System.Windows.Forms.Button $button4 = New-Object System.Windows.Forms.Button $button5 = New-Object System.Windows.Forms.Button $button6 = New-Object System.Windows.Forms.Button $openFileDialog1 = New-Object System.Windows.Forms.OpenFileDialog $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. $button1_click= { $openFileDialog1.ShowDialog()| Out-Null if ($openFileDialog1.filename -ne "浏览目标文件绝对路径") { $script:newfile = 1 $script:myfileobject = New-Object System.IO.FileInfo($openFileDialog1.filename) } } $button2_click= { if ($script:newfile -eq 1) { $RichTextBox1.text = "计算中,请等待..." $RichTextBox1.refresh() $button2.Text = "计算中..." $button2.Enabled = $False $button2.refresh() $button3.Text = "计算中..." $button3.Enabled = $False $button3.refresh() $button4.Text = "计算中..." $button4.Enabled = $False $button4.refresh() $button5.Text = "计算中..." $button5.Enabled = $False $button5.refresh() $Filecontent=$script:myfileobject.OpenRead() $sha1.ComputeHash($Filecontent) | foreach -Begin{$filesha1hashcode=''} -Process{$filesha1hashcode += "{0:x2}" -f $_} -End{$RichTextBox1.text = $openFileDialog1.filename,"`n",$filesha1hashcode; $RichTextBox1.refresh()} $Filecontent.Dispose() $button2.Text = "计算SHA1值" $button2.Enabled = $True $button2.refresh() $button3.Text = "计算SHA256值" $button3.Enabled = $True $button3.refresh() $button4.Text = "计算SHA512值" $button4.Enabled = $True $button4.refresh() $button5.Text = "计算MD5值" $button5.Enabled = $True $button5.refresh() } else { $RichTextBox1.text = "你还没按'打开目标文件'按钮" $RichTextBox1.refresh() } } $button3_click= { if ($script:newfile -eq 1) { $RichTextBox1.text = "计算中,请等待..." $RichTextBox1.refresh() $button2.Text = "计算中..." $button2.Enabled = $False $button2.refresh() $button3.Text = "计算中..." $button3.Enabled = $False $button3.refresh() $button4.Text = "计算中..." $button4.Enabled = $False $button4.refresh() $button5.Text = "计算中..." $button5.Enabled = $False $button5.refresh() $Filecontent=$script:myfileobject.OpenRead() $sha256.ComputeHash($Filecontent) | foreach -Begin{$filesha256hashcode=''} -Process{$filesha256hashcode += "{0:x2}" -f $_} -End{$RichTextBox1.text = $openFileDialog1.filename,"`n",$filesha256hashcode; $RichTextBox1.refresh()} $Filecontent.Dispose() $button2.Text = "计算SHA1值" $button2.Enabled = $True $button2.refresh() $button3.Text = "计算SHA256值" $button3.Enabled = $True $button3.refresh() $button4.Text = "计算SHA512值" $button4.Enabled = $True $button4.refresh() $button5.Text = "计算MD5值" $button5.Enabled = $True $button5.refresh() } else { $RichTextBox1.text = "你还没按'打开目标文件'按钮" $RichTextBox1.refresh() } } $button4_click= { if ($script:newfile -eq 1) { $RichTextBox1.text = "计算中,请等待..." $RichTextBox1.refresh() $button2.Text = "计算中..." $button2.Enabled = $False $button2.refresh() $button3.Text = "计算中..." $button3.Enabled = $False $button3.refresh() $button4.Text = "计算中..." $button4.Enabled = $False $button4.refresh() $button5.Text = "计算中..." $button5.Enabled = $False $button5.refresh() $Filecontent=$script:myfileobject.OpenRead() $sha512.ComputeHash($Filecontent) | foreach -Begin{$filesha512hashcode=''} -Process{$filesha512hashcode += "{0:x2}" -f $_} -End{$RichTextBox1.text = $openFileDialog1.filename,"`n",$filesha512hashcode; $RichTextBox1.refresh()} $Filecontent.Dispose() $button2.Text = "计算SHA1值" $button2.Enabled = $True $button2.refresh() $button3.Text = "计算SHA256值" $button3.Enabled = $True $button3.refresh() $button4.Text = "计算SHA512值" $button4.Enabled = $True $button4.refresh() $button5.Text = "计算MD5值" $button5.Enabled = $True $button5.refresh() } else { $RichTextBox1.text = "你还没按'打开目标文件'按钮" $RichTextBox1.refresh() } } $button5_click= { if ($script:newfile -eq 1) { $RichTextBox1.text = "计算中,请等待..." $RichTextBox1.refresh() $button2.Text = "计算中..." $button2.Enabled = $False $button2.refresh() $button3.Text = "计算中..." $button3.Enabled = $False $button3.refresh() $button4.Text = "计算中..." $button4.Enabled = $False $button4.refresh() $button5.Text = "计算中..." $button5.Enabled = $False $button5.refresh() $Filecontent=$script:myfileobject.OpenRead() $md5.ComputeHash($Filecontent) | foreach -Begin{$filemd5hashcode=''} -Process{$filemd5hashcode += "{0:x2}" -f $_} -End{$RichTextBox1.text = $openFileDialog1.filename,"`n",$filemd5hashcode; $RichTextBox1.refresh()} $Filecontent.Dispose() $button2.Text = "计算SHA1值" $button2.Enabled = $True $button2.refresh() $button3.Text = "计算SHA256值" $button3.Enabled = $True $button3.refresh() $button4.Text = "计算SHA512值" $button4.Enabled = $True $button4.refresh() $button5.Text = "计算MD5值" $button5.Enabled = $True $button5.refresh() } else { $RichTextBox1.text = "你还没按'打开目标文件'按钮" $RichTextBox1.refresh() } } $button6_click= { $RichTextBox1.Selectall() $RichTextBox1.Copy() } $OnLoadForm_StateCorrection= {#Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 700 $System_Drawing_Size.Height = 200 $form1.ClientSize = $System_Drawing_Size $form1.Text = "文件 MD5 SHA1 SHA256 SHA512 校验码生成工具 V1.3" $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.StartPosition = "CenterScreen" $form1.Name = "form1" $richTextBox1.Text = "文件 MD5 SHA1 SHA256 SHA512 校验码生成工具 V1.3" $richTextBox1.BackColor = [System.Drawing.Color]::FromArgb(255,70,130,180) $richTextBox1.Anchor = 15 $richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 0 $System_Drawing_Point.Y = 34 $richTextBox1.Location = $System_Drawing_Point $richTextBox1.Name = "richTextBox1" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 701 $System_Drawing_Size.Height = 168 $richTextBox1.ScrollBars = 18 $richTextBox1.Size = $System_Drawing_Size $richTextBox1.TabIndex = 0 $richTextBox1.Font = New-Object System.Drawing.Font("MS UI Gothic",24,0,3,0) $form1.Controls.Add($richTextBox1) $button1.Text = "打开目标文件" $button1.UseVisualStyleBackColor = $True $button1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 0 $System_Drawing_Point.Y = 0 $button1.Location = $System_Drawing_Point $button1.Name = "button1" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 120 $System_Drawing_Size.Height = 25 $button1.Size = $System_Drawing_Size $button1.TabIndex = 1 $button1.add_Click($button1_click) $form1.Controls.Add($button1) $button2.Text = "计算SHA1值" $button2.UseVisualStyleBackColor = $True $button2.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 120 $System_Drawing_Point.Y = 0 $button2.Location = $System_Drawing_Point $button2.Name = "button2" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 80 $System_Drawing_Size.Height = 25 $button2.Size = $System_Drawing_Size $button2.TabIndex = 2 $button2.add_Click($button2_click) $form1.Controls.Add($button2) $button3.Text = "计算SHA256值" $button3.UseVisualStyleBackColor = $True $button3.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 200 $System_Drawing_Point.Y = 0 $button3.Location = $System_Drawing_Point $button3.Name = "button3" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 80 $System_Drawing_Size.Height = 25 $button3.Size = $System_Drawing_Size $button3.TabIndex = 3 $button3.add_Click($button3_click) $form1.Controls.Add($button3) $button4.Text = "计算SHA512值" $button4.UseVisualStyleBackColor = $True $button4.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 280 $System_Drawing_Point.Y = 0 $button4.Location = $System_Drawing_Point $button4.Name = "button4" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 80 $System_Drawing_Size.Height = 25 $button4.Size = $System_Drawing_Size $button4.TabIndex = 4 $button4.add_Click($button4_click) $form1.Controls.Add($button4) $button5.Text = "计算MD5值" $button5.UseVisualStyleBackColor = $True $button5.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 360 $System_Drawing_Point.Y = 0 $button5.Location = $System_Drawing_Point $button5.Name = "button5" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 80 $System_Drawing_Size.Height = 25 $button5.Size = $System_Drawing_Size $button5.TabIndex = 5 $button5.add_Click($button5_click) $form1.Controls.Add($button5) $button6.Text = "结果复制到剪贴板" $button6.UseVisualStyleBackColor = $True $button6.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 440 $System_Drawing_Point.Y = 0 $button6.Location = $System_Drawing_Point $button6.Name = "button6" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 120 $System_Drawing_Size.Height = 25 $button6.Size = $System_Drawing_Size $button6.TabIndex = 6 $button6.add_Click($button6_click) $form1.Controls.Add($button6) $openFileDialog1.FileName = "浏览目标文件绝对路径" $openFileDialog1.InitialDirectory = "Application.StartupPath" $openFileDialog1.ShowHelp = $True #endregion Generated Form Code #Save the initial state of the form $InitialFormWindowState = $form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($OnLoadForm_StateCorrection) #Show the Form $form1.ShowDialog()| Out-Null } function get-hash-md5 { $md5 = [System.Security.Cryptography.MD5]::Create() $script:myfileobject = New-Object System.IO.FileInfo($script:filename) $Filecontent=$script:myfileobject.OpenRead() $md5.ComputeHash($Filecontent) | foreach -Begin{$filemd5hashcode=''} -Process{$filemd5hashcode += "{0:x2}" -f $_} -End{write-host $script:filename `n$filemd5hashcode} $Filecontent.Dispose() } function get-hash-sha1 { $sha1 = [System.Security.Cryptography.SHA1]::Create() $script:myfileobject = New-Object System.IO.FileInfo($script:filename) $Filecontent=$script:myfileobject.OpenRead() $sha1.ComputeHash($Filecontent) | foreach -Begin{$filesha1hashcode=''} -Process{$filesha1hashcode += "{0:x2}" -f $_} -End{write-host $script:filename `n$filesha1hashcode} $Filecontent.Dispose() } function get-hash-sha256 { $sha1 = [System.Security.Cryptography.SHA256]::Create() $script:myfileobject = New-Object System.IO.FileInfo($script:filename) $Filecontent=$script:myfileobject.OpenRead() $sha1.ComputeHash($Filecontent) | foreach -Begin{$filesha1hashcode=''} -Process{$filesha1hashcode += "{0:x2}" -f $_} -End{write-host $script:filename `n$filesha1hashcode} $Filecontent.Dispose() } function get-hash-sha512 { $sha1 = [System.Security.Cryptography.SHA512]::Create() $script:myfileobject = New-Object System.IO.FileInfo($script:filename) $Filecontent=$script:myfileobject.OpenRead() $sha1.ComputeHash($Filecontent) | foreach -Begin{$filesha1hashcode=''} -Process{$filesha1hashcode += "{0:x2}" -f $_} -End{write-host $script:filename `n$filesha1hashcode} $Filecontent.Dispose() } #endregion #region main if (!$args) { get-hash-window } else { Write-Host `n"文件md5,sha1,sha256,sha512校验码生成工具 V1.3"`n if (!$args[1]) {"第二个参数输入错误:你没输入要校验的文件"} else { if ($script:filename=Resolve-Path $args[1]) { if ($args[0] -eq "-sha1") {get-hash-sha1} elseif ($args[0] -eq "-sha256") {get-hash-sha256} elseif ($args[0] -eq "-sha512") {get-hash-sha512} elseif ($args[0] -eq "-md5") {get-hash-md5} else { echo "第一个参数输入错误:参数应该是 '-sha1', '-sha256','-sha512','-md5'" } } else {throw "第二个参数输入错误:找不到这个文件 $args[1]" } } } #endregion
如果连powershell都不支持的系统的话,比如win xp,那么需要下载安装 KB968930 这个补丁就可以啦,也就是这个文件:WindowsXP-KB968930-x86-CHS.exe,可以自己去微软官方下载,也可以在下面的百度盘链接中下载。
百度盘下载链接:文件_MD5_SHA1_SHA256_SHA512_校验码生成工具V1.3
另外本站这篇文章你可能感兴趣:微软出品:MD5和SHA1值验证命令行工具
《PowerShell 4.0 新增计算文件hash值的命令:Get-FileHash》有一个想法
评论已关闭。