A hash is a way to uniquely identify a text without exposing the actual text. Hashes are used to identify texts, find duplicate file content, and validate passwords. PowerShell 5 and better even comes with a cmdlet to calculate hash values for files: Get-FileHash.
However, Get-FileHash has no way of calculating hashes from strings. Instead of saving string values to file just to calculate the hash value, you can use a so-called memory stream instead. Here is a piece of code that calculates a hash from any string:
$Text = 'this is the text that you want to convert into a hash' $stream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($Text)) $hash = Get-FileHash -InputStream $stream -Algorithm SHA1 $stream.Close() $stream.Dispose() $hash
Don’t forget to close and dispose the memory stream after use to prevent memory leaks and free all resources.
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!