Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use Get-FileHash in Windows PowerShell

The Get-FileHash cmdlet in Windows PowerShell is an essential tool for verifying the integrity of files by computing their hash values. This is particularly important for ensuring that files have not been tampered with or corrupted during transfer. Hash values are unique strings generated from the contents of a file, and any change in the file's content will result in a different hash value. This article will guide you through the process of using Get-FileHash in a Windows environment, providing practical examples and use cases.


Examples:


1. Basic Usage of Get-FileHash:
To compute the hash value of a file, you can use the Get-FileHash cmdlet followed by the path to the file. By default, Get-FileHash uses the SHA256 algorithm.


   Get-FileHash -Path "C:\path\to\your\file.txt"

This command will output the hash value of the specified file.


2. Using Different Hash Algorithms:
Get-FileHash supports several hash algorithms such as SHA1, SHA384, SHA512, and MD5. You can specify the algorithm using the -Algorithm parameter.


   Get-FileHash -Path "C:\path\to\your\file.txt" -Algorithm SHA512

This command will compute the SHA512 hash of the file.


3. Comparing Hash Values:
To verify the integrity of a file, you can compare its hash value with a known good hash. Here’s an example of how to do this:


   $originalHash = "known_good_hash_value"
$computedHash = Get-FileHash -Path "C:\path\to\your\file.txt" -Algorithm SHA256

if ($computedHash.Hash -eq $originalHash) {
Write-Output "The file is intact."
} else {
Write-Output "The file has been altered."
}

This script compares the computed hash of the file with a known good hash and outputs whether the file is intact or has been altered.


4. Hashing Multiple Files:
You can also compute the hash values of multiple files in a directory by using a loop.


   Get-ChildItem -Path "C:\path\to\your\directory" | ForEach-Object {
Get-FileHash -Path $_.FullName
}

This command will compute and display the hash values of all files in the specified directory.


To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.