Trouble creating a hash comparison script

I am trying to create a PS script that compares the hash of a file after download to ensure integrity. I want to make it user friendly, something I can stick in our RMM tool set so lower tier technicians can utilize it. I originally wanted to create variables like:

$1 = SHA1
$2 = SHA256
$3 = SHA384
$4 = SHA512
$5 = MD5

but I get the following message:

The term ‘SHA1’ is not recognized as the name of a cmdlet, function,
| script file, or operable program. Check the spelling of the name, or
| if a path was included, verify that the path is correct and try again.

When I try the variable as a string

$1 = "SHA1" $2 = "SHA256" $3 = "SHA384" $4 = "SHA512" $5 = "MD5"
There is no error message. I figured this is because I am trying to define something already defined, or alpha numeric variables are not allowed. I am not asking for this to be done for me, just to be pointed in the right direction so I can accomplish the following actions:
  • User is prompted for a value 1-5, each representing an Algorithm
  • User provides file path
  • User inserts hash comparison
  • User is presented with a Boolean value stating if there is a match or not
 

I am an aspiring DevOps IT, I am admittedly new to robust scripting but I catch on quick. I have some experience programming with full stack environments. Any advise would be greatly appreciated.

Hello NinemmRx,

When you are initializing variables the 1st time PowerShell treats values after = as cmdlets not variable values. The error message states exactly that.

When you are doing it 2nd time you are initializing 5 string variables.

Reference: about_Variables

Hope that helps.

 

As Andy has said, without qoutes Powershell believes you are calling a command. Here are a couple of better approaches as well:

Array and Indexes:

$algorithm = 'SHA1','SHA256','SHA384','SHA512','MD5'

$algorithm | foreach{'({0}) - {1}' -f ($algorithm.IndexOf($_) + 1), $_}

$choice = Read-Host 'Choose a number (1-5) of the algorithm to use'

Get-FileHash -Path C:\Scripts\random.txt -Algorithm $algorithm[$choice - 1]

Hash Table:

$algorithm = [ordered]@{
    1 = 'SHA1'
    2 = 'SHA256'
    3 = 'SHA384'
    4 = 'SHA512'
    5 = 'MD5'
}

$algorithm.GetEnumerator() | foreach {'({0}) - {1}' -f $_.Key, $_.Value}

$choice = Read-Host 'Choose a number (1-5) of the algorithm to use'

Get-FileHash -Path C:\Scripts\random.txt -Algorithm $algorithm.Item(2)