Get-FileHash (Microsoft.PowerShell.Utility) - PowerShell

分类: 365视频游戏世界 时间: 2025-08-07 17:37:54 作者: admin 阅读: 7189 点赞: 58
Get-FileHash (Microsoft.PowerShell.Utility) - PowerShell

Get-FileHash

模块:

Microsoft.PowerShell.Utility Module

Computes the hash value for a file by using a specified hash algorithm.

语法

Path (默认值)

Get-FileHash

[-Path]

[[-Algorithm] ]

[]

LiteralPath

Get-FileHash

[-LiteralPath]

[[-Algorithm] ]

[]

StreamParameterSet

Get-FileHash

[-InputStream]

[[-Algorithm] ]

[]

说明

The Get-FileHash cmdlet computes the hash value for a file by using a specified hash algorithm.

A hash value is a unique value that corresponds to the content of the file. Rather than identifying

the contents of a file by its file name, extension, or other designation, a hash assigns a unique

value to the contents of a file. File names and extensions can be changed without altering the

content of the file, and without changing the hash value. Similarly, the file's content can be

changed without changing the name or extension. However, changing even a single character in the

contents of a file changes the hash value of the file.

The purpose of hash values is to provide a cryptographically-secure way to verify that the contents

of a file have not been changed. While some hash algorithms, including MD5 and SHA1, are no longer

considered secure against attack, the goal of a secure hash algorithm is to render it impossible to

change the contents of a file -- either by accident, or by malicious or unauthorized attempt -- and

maintain the same hash value. You can also use hash values to determine if two different files have

exactly the same content. If the hash values of two files are identical, the contents of the files

are also identical.

By default, the Get-FileHash cmdlet uses the SHA256 algorithm, although any hash algorithm that

is supported by the target operating system can be used.

示例

Example 1: Compute the hash value for a file

This example uses the Get-FileHash cmdlet to compute the hash value for the

/etc/apt/sources.list file. The hash algorithm used is the default, SHA256. The output is

piped to the Format-List cmdlet to format the output as a list.

Get-FileHash /etc/apt/sources.list | Format-List

Algorithm : SHA256

Hash : 3CBCFDDEC145E3382D592266BE193E5BE53443138EE6AB6CA09FF20DF609E268

Path : /etc/apt/sources.list

Example 2: Compute the hash value for an ISO file

This example uses the Get-FileHash cmdlet and the SHA384 algorithm to compute the hash value

for an ISO file that an administrator has downloaded from the internet. The output is piped to the

Format-List cmdlet to format the output as a list.

Get-FileHash C:\Users\user1\Downloads\Contoso8_1_ENT.iso -Algorithm SHA384 | Format-List

Algorithm : SHA384

Hash : 20AB1C2EE19FC96A7C66E33917D191A24E3CE9DAC99DB7C786ACCE31E559144FEAFC695C58E508E2EBBC9D3C96F21FA3

Path : C:\Users\user1\Downloads\Contoso8_1_ENT.iso

Example 3: Compute the hash value of a stream

For this example, we are using System.Net.WebClient to download a package from the

PowerShell release page. The release

page also documents the SHA256 hash of each package file. We can compare the published hash value

with the one we calculate with Get-FileHash.

$wc = [System.Net.WebClient]::new()

$baseurl = 'https://github.com/PowerShell/PowerShell/releases/download/v6.2.4/'

$pkgname = 'powershell_6.2.4-1.debian.9_amd64.deb'

$pkgurl = $baseurl + $pkgname

$publishedHash = '8E28E54D601F0751922DE24632C1E716B4684876255CF82304A9B19E89A9CCAC'

$FileHash = Get-FileHash -InputStream ($wc.OpenRead($pkgurl))

$FileHash.Hash -eq $publishedHash

True

Example 4: Compute the hash of a string

PowerShell does not provide a cmdlet to compute the hash of a string. However, you can write a

string to a stream and use the InputStream parameter of Get-FileHash to get the hash value.

$stringAsStream = [System.IO.MemoryStream]::new()

$writer = [System.IO.StreamWriter]::new($stringAsStream)

$writer.Write("Hello world")

$writer.Flush()

$stringAsStream.Position = 0

Get-FileHash -InputStream $stringAsStream | Select-Object Hash

Hash

----

64EC88CA00B268E5BA1A35678A1B5316D212F4F366B2477232534A8AECA37F3C

参数

-Algorithm

Specifies the cryptographic hash function to use for computing the hash value of the contents of the

specified file or stream. A cryptographic hash function has the property that it is infeasible to

find two different files with the same hash value. Hash functions are commonly used with digital

signatures and for data integrity. The acceptable values for this parameter are:

SHA1

SHA256

SHA384

SHA512

MD5

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

For security reasons, MD5 and SHA1, which are no longer considered secure, should only be used for

simple change validation, and should not be used to generate hash values for files that require

protection from attack or tampering.

参数属性

类型:String

默认值:SHA256

接受的值:SHA1, SHA256, SHA384, SHA512, MD5

支持通配符:False

不显示:False

参数集

(All)

Position:1

必需:False

来自管道的值:False

来自管道的值(按属性名称):False

来自剩余参数的值:False

-InputStream

Specifies the input stream.

参数属性

类型:Stream

默认值:None

支持通配符:False

不显示:False

参数集

StreamParameterSet

Position:0

必需:True

来自管道的值:False

来自管道的值(按属性名称):False

来自剩余参数的值:False

-LiteralPath

Specifies the path to a file. Unlike the Path parameter, the value of the LiteralPath

parameter is used exactly as it is typed. No characters are interpreted as wildcard characters. If

the path includes escape characters, enclose the path in single quotation marks. Single quotation

marks instruct PowerShell not to interpret characters as escape sequences.

参数属性

类型:String[]

默认值:None

支持通配符:False

不显示:False

别名:PSPath, LP

参数集

LiteralPath

Position:0

必需:True

来自管道的值:False

来自管道的值(按属性名称):True

来自剩余参数的值:False

-Path

Specifies the path to one or more files as an array. Wildcard characters are permitted.

参数属性

类型:String[]

默认值:None

支持通配符:True

不显示:False

参数集

Path

Position:0

必需:True

来自管道的值:True

来自管道的值(按属性名称):True

来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,

-InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable,

-ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see

about_CommonParameters.

输入

String

You can pipe a string containing a path to a file to this cmdlet.

输出

Microsoft.PowerShell.Utility.FileHash

This cmdlet returns an object representing the path to the specified file, the value of the

computed hash, and the algorithm used to compute the hash.

相关链接

Format-List

相关推荐

在Steam买哪个GTA5版本更合适?详尽解读!
365bet新地址

在Steam买哪个GTA5版本更合适?详尽解读!

📅 07-21 👁️ 1696
安心签是什么平台(安心签—深度解析这款电子签名工具的功能与优势)
为什么说孙杨败诉被禁赛并不冤枉? 世界反兴奋剂组织(WADA)向国际体育仲裁院(CAS)诉孙杨与国际泳联(FINA)一案(CAS 2019/A/6148)...