none
Retreive Windows 10 key and save to file. RRS feed

  • Question

  • Hi, looking for some help on a script I am working on. The VBscript currently, locates and records the PC's Windows 10 key, and also creates a new txt file, however I cant get the file to record the key inside of it (fairly new to all this)

    Also %username% or %computername% are not working for me like they have in other scripts.

    Line 34 & 36 are where the issues are, also what do I need to remove to stop the script showing the code to the screen?

    ' SET VARIABLES
    Set WshShell = CreateObject("WScript.Shell")
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))

    'GATER WINDOWS KEY

    Function ConvertToKey(Key)
    Const KeyOffset = 52
    i = 28
    Chars = "BCDFGHJKMPQRTVWXY2346789"
    Do
    Cur = 0
    x = 14
    Do
    Cur = Cur * 256
    Cur = Key(x + KeyOffset) + Cur
    Key(x + KeyOffset) = (Cur \ 24) And 255
    Cur = Cur Mod 24
    x = x -1
    Loop While x >= 0
    i = i -1
    KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
    If (((29 - i) Mod 6) = 0) And (i <> -1) Then
    i = i -1
    KeyOutput = "-" & KeyOutput
    End If
    Loop While i >= 0
    ConvertToKey = KeyOutput
    End Function

    ' CREATE TEXT FILE

    outFile="F:\IT Support\Win10Keys\" & %computername% & "key.txt"
    Set objFile = objFSO.CreateTextFile(outFile,True)
    objFile.Write %computername% & ConvertToKey & vbCrLf
    objFile.Close

    Thanks for any help :)

    Tuesday, January 16, 2018 10:25 AM

Answers

  • function ConvertTo-ProductKey {
    	param (
    		[parameter(Mandatory = $True, Position = 0)]
    		$Registry,
    		[parameter()]
    		[Switch]$x64
    	)
    	begin {
    		$map = "BCDFGHJKMPQRTVWXY2346789"
    	}
    	process {
    		$ProductKey = ""
    		
    		$prodkey = $Registry[0x34 .. 0x42]
    		
    		for ($i = 24; $i -ge 0; $i--) {
    			$r = 0
    			for ($j = 14; $j -ge 0; $j--) {
    				$r = ($r * 256) -bxor $prodkey[$j]
    				$prodkey[$j] = [math]::Floor([double]($r/24))
    				$r = $r % 24
    			}
    			$ProductKey = $map[$r] + $ProductKey
    			if (($i % 5) -eq 0 -and $i -ne 0) {
    				$ProductKey = "-" + $ProductKey
    			}
    		}
    		$ProductKey
    	}
    }
    
    $x = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -name DigitalProductId
    $key = ConvertTo-ProductKey $x.DigitalProductId
    $outFile="F:\IT Support\Win10Keys\$($env.COMPUTERNAME)_key.txt"
    $key | Out-File $outfile


    \_(ツ)_/


    • Edited by jrv Tuesday, January 16, 2018 10:45 AM
    • Proposed as answer by BOfH-666 Tuesday, January 16, 2018 11:29 AM
    • Marked as answer by Richard MuellerMVP, Moderator Saturday, January 27, 2018 1:14 PM
    Tuesday, January 16, 2018 10:44 AM

All replies

  • function ConvertTo-ProductKey {
    	param (
    		[parameter(Mandatory = $True, Position = 0)]
    		$Registry,
    		[parameter()]
    		[Switch]$x64
    	)
    	begin {
    		$map = "BCDFGHJKMPQRTVWXY2346789"
    	}
    	process {
    		$ProductKey = ""
    		
    		$prodkey = $Registry[0x34 .. 0x42]
    		
    		for ($i = 24; $i -ge 0; $i--) {
    			$r = 0
    			for ($j = 14; $j -ge 0; $j--) {
    				$r = ($r * 256) -bxor $prodkey[$j]
    				$prodkey[$j] = [math]::Floor([double]($r/24))
    				$r = $r % 24
    			}
    			$ProductKey = $map[$r] + $ProductKey
    			if (($i % 5) -eq 0 -and $i -ne 0) {
    				$ProductKey = "-" + $ProductKey
    			}
    		}
    		$ProductKey
    	}
    }
    
    $x = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -name DigitalProductId
    $key = ConvertTo-ProductKey $x.DigitalProductId
    $outFile="F:\IT Support\Win10Keys\$($env.COMPUTERNAME)_key.txt"
    $key | Out-File $outfile


    \_(ツ)_/


    • Edited by jrv Tuesday, January 16, 2018 10:45 AM
    • Proposed as answer by BOfH-666 Tuesday, January 16, 2018 11:29 AM
    • Marked as answer by Richard MuellerMVP, Moderator Saturday, January 27, 2018 1:14 PM
    Tuesday, January 16, 2018 10:44 AM
  • Thanks for this, an error comes up.

    It says '(' is expected on Line 1, Char 19

    Tuesday, January 16, 2018 10:57 AM
  • It is not VBScript

    \_(ツ)_/

    Tuesday, January 16, 2018 11:07 AM
  • Sorry, what kind of script is it? I am new to this, .bat dosent work either
    Tuesday, January 16, 2018 11:12 AM
  • VBScript is pretty much obsolete. It is PowerShell.


    \_(ツ)_/

    Tuesday, January 16, 2018 11:13 AM
  • Thank you! Runs in powershell great, just the file saves as "_key.txt" only, it dosent attatch the PC's name.

    Also if i place the .ps1 file into the startup folder it should just run like normal upon starting the PC?

    Tuesday, January 16, 2018 11:18 AM
  • In VBScript you cannot use environment variables from batch files.

    Also note that we do not fix scripts found on the Internet.  You must learn how to write a script and post an issue that you have.  You are just adding things blindly.


    \_(ツ)_/

    Tuesday, January 16, 2018 11:18 AM
  • Yes you're right, unfortunately thats the way im learning just following tutorials and online forums, and frankensteining scripts together. (ive made about 12 for this new job)

    Thank you for your expertise thus far,

    If you could assist me in understanding the reason why the computer name isnt showing on the text file name, would be appreciated.

    Tuesday, January 16, 2018 11:29 AM
  • Change the dot to a colon and it will work.

    Script Gallery.

    Learn PowerShell  

    Script requests


    \_(ツ)_/

    Tuesday, January 16, 2018 11:36 AM
  • Beautiful, thank you buddy. Hope this thread will now help anyone else :)
    Tuesday, January 16, 2018 11:39 AM
  • The product key function is in the Gallery.  If you search with your favorite search engine you will find 100+ versions of it.

    \_(ツ)_/

    Tuesday, January 16, 2018 11:41 AM
  • Hi again, I'm using this same script to try and retrieve all the products keys from multiple PC's and place them automatically onto the network drive "F:" however every PC I use it on is returning the same product key.

    As far I was concerned the line "$x = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -name DigitalProductId" retrieves the product key from the host machine.

    This would make sense to why I was getting the same product key on every PC as the script is stored on the F: drive server, however, even If I place the script directly onto the users hard drive and run from there it still shows the same product key. Do you know what I am missing?

    Tuesday, January 16, 2018 3:20 PM
  • You cannot run this remotely.  It only works on the local system.  You can use a remote session to run the script..

    help Invoke-Command -Online


    \_(ツ)_/

    Tuesday, January 16, 2018 3:26 PM
  • So I place the script physically on 1 PC, and then did the same to another. Why is this still showing the same product key for both? 
    Tuesday, January 16, 2018 3:36 PM
  • You have to go physically to the remote PC and log in to run the script.

    If you have bulk licensing then all keys would be the same.  If you install using the same key on multiple systems then they will have the same key.


    \_(ツ)_/

    Tuesday, January 16, 2018 3:39 PM
  • Yes, I'm physically walking across to another computer, taking the script off the shared drive and placing it onto the desktop and then running it.

    5/6 different PC's are all returning the same product key, and they all definitely have different keys.

    I'm not sure where the system is pulling this product key from.

    Tuesday, January 16, 2018 3:46 PM
  • This is not a scripting issue.  If they all have the same keys then what makes you think they have different keys?


    \_(ツ)_/

    Tuesday, January 16, 2018 3:50 PM
  • Because I purchased the keys, and installed onto all the PC's. 

    On top of this, produkey software is showing the correct (different) keys. 

    I agree its not a scripting issue, I just feel like the application is always taking the product key from the same place I cant figure out where or why.


    Also it is not the generic product key that shows up when you upgade to windows 10
    • Edited by Nabsal Tuesday, January 16, 2018 3:59 PM
    Tuesday, January 16, 2018 3:56 PM
  • On Windows 10 and later you can use this:

    (Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey


    \_(ツ)_/

    Tuesday, January 16, 2018 4:00 PM