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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
Thanks
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
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
the only error left is being able to run it as an admin
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
$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
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--