--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools



Would you like to sponsor this site?
Or buy me a beer?:

Recently Changed Pages:

View All Pages
View All Q Pages

View All Tags

Sign up for Q to post comments.

WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.

Terms And Conditions for Q users

Pages with comments

2019/06/22 10:36 1 Comment
2019/03/15 16:02 1 Comment
2019/03/15 16:02 1 Comment
2019/03/15 16:02 3 Comments
2017/04/20 15:28 1 Comment
2017/04/20 15:23 1 Comment
2017/04/19 14:44 1 Comment
2017/04/17 20:10 1 Comment
2017/04/17 20:07 1 Comment
2017/04/17 19:58 1 Comment
2017/04/17 19:52 1 Comment

View All Comments


PowerCLI - Use MD5 Hashing

This also can be used in regular powershell of course, but this is a short howto on using MD5 hashing in powercli scripts. It is based on this script from WinTellect.

First step is to create the Get-Hash.psm1 file. This can be a regular textfile and should contain the following lines:

    Gets the hash value
    Gets the hash value of a file or string
    It uses System.Security.Cryptography.HashAlgorithm (
    and FileStream Class (
    Written by Josep Martínez Vilà:
    Based on: and some ideas on Microsoft Online Help
    File to get the hash from.
    Text string to get the hash from
.PARAMETER Algorithm
    Type of hash algorithm to use. Default is SHA1
    C:\PS> Get-Hash "myFile.txt"
    Gets the SHA1 from myFile.txt file. When there's no explicit parameter, it uses -File
    Get-Hash -File "C:\temp\myFile.txt"
    Gets the SHA1 from myFile.txt file
    C:\PS> Get-Hash -Algorithm "MD5" -Text "Hello Wold!" 
    Gets the MD5 from a string
    C:\PS> "Hello Wold!" | Get-Hash
    We can pass a string throught the pipeline    
function Get-Hash
        [parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="set1")]
        [parameter(Position=0, Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="set2")]
        $file = "",
        [parameter(Mandatory=$false, ValueFromPipeline=$false)]
        [ValidateSet("MD5", "SHA", "SHA1", "SHA-256", "SHA-384", "SHA-512")]
        $algorithm = "SHA1"
        $hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create($algorithm)
        $md5StringBuilder = New-Object System.Text.StringBuilder 50
        $ue = New-Object System.Text.UTF8Encoding 
        if ($file){
            try {
                if (!(Test-Path $file)){
                    throw "Test-Path returned false."
            catch {
                throw "Get-Hash - File not found or without permisions: [$file]. $_"
            try {        
                [System.IO.FileStream]$fileStream = [System.IO.File]::Open($file, [System.IO.FileMode]::Open);
                $hashAlgorithm.ComputeHash($fileStream) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
            catch {
                throw "Get-Hash - Error reading or hashing the file: [$file]"
            finally {
        else {
            $hashAlgorithm.ComputeHash($ue.GetBytes($text)) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
        return $md5StringBuilder.ToString()

Now you can use it in your script by first importing the script as a module:

# Add Get-Hash function
Import-Module g:\scripts\get-hash.psm1

Now you can create a string, for example based on the VM name with an extra string:

$stringtohash = $vm.toUpper() + "-go4shift"
$guestpw = (Get-Hash -Algorithm "MD5" -text $stringtohash)

or like this to add extra parameters after hashing:

$guestpw = (Get-Hash -Algorithm "MD5" -text $stringtohash) + "!!!"

Now you can use the password like you would normally do, for example when using the Copy-VMGuestFile commandlet:

Copy-VMGuestFile -Source $scriptlocation\$script -Destination $guestdir -Force -VM $vm -LocalToGuest -HostUser root -HostPassword $hostpw -GuestUser Administrator -GuestPassword $guestpw
You could leave a comment if you were logged in.
powerclimd5hash.txt · Last modified: 2014/09/23 13:29 by sjoerd