need help on powershell script

Sébastien Rousseau 1 Reputation point

Hello all,

I'm starting to use powershell and i'm trying to get a certain results and i'm sure someone would be able to help me.

i'm using this command to get the Windows10 cd-key : wmic path softwarelicensingservice get OA3xOriginalProductKey

i would like to copy the results of this request and add it to this line : slmgr.vbs -ipk ( insert cd-key here )

i need to find a way to have this process automated


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,354 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. MotoX80 31,561 Reputation points

    Something like this?

    (Get-CimInstance -ClassName softwarelicensingservice).OA3xOriginalProductKey | Out-File 'C:\temp\key.txt'    #Fixed
    $key = Get-Content 'C:\temp\key.txt'
    cscript.exe C:\Windows\System32\slmgr.vbs -ipk $key 

  2. Sébastien Rousseau 1 Reputation point

    there is unwanted stuff in the key.txt

    This is the first line in the text file : OA3xOriginalProductKey

    the info i need ( the actual cd-key ) is typed after that

  3. Sébastien Rousseau 1 Reputation point

    the only error left is being able to run it as an admin

    0 comments No comments

  4. MotoX80 31,561 Reputation points

    Well that really isn't an error, and all the user has to do is launch the .bat or .ps1 with "run as administrator".

    To do it programmatically is a bit of a challenge due to PS restrictions.

    # Save the key to a file 
    $key = (Get-CimInstance -ClassName softwarelicensingservice).OA3xOriginalProductKey 
    $key | Out-File 'C:\temp\key.txt'
    # Start-Process doesn't like redirection with elevation. 
    # Elevate a second Powershell process and have it call the script with redirection. 
    $stdout = "$env:temp\stdout.txt"
    $stderr = "$env:temp\stderr.txt"
    $key = Get-Content 'C:\temp\key.txt'
    $cmd = "start-process cscript.exe -argumentlist 'C:\Windows\System32\slmgr.vbs -ipk $key' -nonewwindow -wait -RedirectStandardOutput $stdout -RedirectStandardError $stderr"
    Start-Process 'powershell.exe' -argumentlist $cmd -wait -verb runas  
    Get-Content $stdout
    Get-Content $stderr
    0 comments No comments

  5. Limitless Technology 39,336 Reputation points

    Hello SbastienRousseau

    Basically you'll need to assign a variable to the next powershell cmdlet to extract the key:

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

    then use the variable in the cmdlet: Set-WindowsProductKey -ProductKey $yourvariable

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments