How to get the Hash of a file with PowerShell

When downloading files from the internet, its important to be sure that the file hasn’t been modified in anyway. Certain websites will profile a ‘hash’ of the file either on the website, or sometimes in a separate text file that can also be downloaded.

Once the file has been downloaded to your workstation, you need to create your own hash of the file, then compare the two to make sure they match. If one tiny change has been made to the file, even an extra space somewhere, the hash will be completely different.

To get the hash of a file, you can use the script below. Copy the text into PowerShell_ISE and save it as Get-FileHash.ps1 (or something along those lines).

Parameters
-FilePath
This is a Mandatory parameter
This is the path to the file that you want to create the hash of.

-HashingAlgorithm
This is NOT mandatory, but if not specified, the default option of SHA256 will be used
This is the Algorithm used when encrypting your file to create the hash.
Valid options are MD5, SHA1, SHA256, SHA384 and SHA512.

Example Usage

Default options, just specifying a path to a file.

Specifying a different Hashing Algorithm.

Test if PowerShell has been run as Administrator

A quick utility function to add into your scripts to test whether or not PowerShell has been run as Administrator.

The function returns a boolean value ($True or $False).

Example Usage

Monty Hall Problem – Test Script

A quick script knocked up to test the theories in the Monty Hall Problem. Defaults to run 1000 tests, but this can be adjusted using the $NumberOfTests parameter.

A second parameter controls the choice of whether to switch doors, or stick with the same one. This is the $Choice parameter and can accept the following options. The default option is LetFateDecide.

  • StickWithDoor
  • SwitchDoor
  • LetFateDecide

Examples

Runs 1000 instances of the Monty Hall Problem

Runs 1000 instances of the Monty Hall Problem where the door gets switched every time

Runs 5000 instances of the Monty Hall Problem, sticking with the same door everytime.


Full ScriptTest-MontyHallProblem.ps1