1. 程式人生 > >使用PowerShell 的Get-FileHash 命令為檔案生成校驗碼

使用PowerShell 的Get-FileHash 命令為檔案生成校驗碼

PowerShell is an automation platform and scripting language for Windows and Windows Server that allows you to simplify the management of your systems. Unlike other text-based shells, PowerShell harnesses the power of the .NET Framework, providing rich objects and a massive set of built-in functionality for taking control of your Windows environments. 

The Get-FileHash command in PowerShell can be used to computes the hash value for a file by using a specified hash algorithm.

The acceptable values for hash algorithm is:

  • SHA1
  • SHA256
  • SHA384
  • SHA512
  • MACTripleDES
  • MD5
  • RIPEMD160

If no value is specified, or if the parameter is omitted, the default value is SHA256.

Note: 

1.The Get-FileHash command is only supported by PowerShell 4.0 or above. By default, this command is not supported by the Powershell version on Windows 7. If the command is not supported, upgrade you powershell. Here is the Powershell 5.1 upgrade package download link:https://www.microsoft.com/en-us/download/details.aspx?id=54616

2. In the file path parameter, make sure there is no space characters, or there will be errors.

Below is the SYNTAX of the Get-FileHash command, See the following documentation in detail on how to do it.

SYNTAX

Path (Default)

Get-FileHash [-Path] <String[]> [-Algorithm <String>] [<CommonParameters>]

LiteralPath

Get-FileHash -LiteralPath <String[]> [-Algorithm <String>] [<CommonParameters>]

Stream

Get-FileHash -InputStream <Stream> [-Algorithm <String>] [<CommonParameters>]
  • Examples:

1.Use powershell to calculate the MD5 checksum.

PS C:\Windows\system32> Get-FileHash C:\Users\212465406\Desktop\Newfolder\3-ECM-CHECKSUM\V3.0\XXX_Setup.exe -Algorithm MD5 | Format-List
Algorithm : MD5
Hash : 829F1E0E2A22E151028E6C1E36FAB074
Path : C:\Users\212465406\Desktop\Newfolder\3-ECM-CHECKSUM\V3.0\XXX_Setup.exe

2.Use powershell to calculate the SHA256 checksum.

PS C:\Windows\system32> Get-FileHash C:\Users\212465406\Desktop\Newfolder\3-ECM-CHECKSUM\V3.0\XXX_Setup.exe -Algorithm SHA256 | Format-List
Algorithm : SHA256
Hash : FAFD14CD93AF5395A7CA71BD90DC258527EED98CCCC856B371E23576A81BD29C
Path : C:\Users\212465406\Desktop\Newfolder\3-ECM-CHECKSUM\V3.0\XXX_Setup.exe

3.Below is the screen-shot of the above commands:

Limitation: Currently, the PowerShell Get-FileHash command not supports to generates hash code for folders or group of files, e.g. Files and folders on one disk.