Search code examples
windowscmdms-office

How to get last 5 character Office license key only using script


I use the below command to get the last 5 char of the office key.

cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus >> key.txt

Result in the text file:

Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved.

---Processing--------------------------
--------------------------------------- PRODUCT ID: 00216-40000-00000-AA541 SKU ID: b322da9c-a2e2-4058-9e4e-f59a6970bd69 LICENSE NAME: Office 15, OfficeProPlusVL_KMS_Client edition LICENSE DESCRIPTION: Office 15, VOLUME_KMSCLIENT channel LICENSE STATUS: 
---LICENSED---  Last 5 characters of installed product key: GVGXT ........

How I can get "GVGXT" only instead of getting a lot of information. I was trying to read the key.txt and only get "GVGXT" then store in csv.


Solution

  • It will return the last key appended on key.txt,

    @echo off
    
    setlocal
    
    for /f "tokens=2 delims=:" %%G IN ('findstr /C:"Last 5 characters of installed product key:" ^<key.txt') do ( set "key=%%G" )
    set "key=%key: =%"
    echo %key%
    

    We can go without a file,

    @echo off
    
    setlocal
    
    for /f "tokens=2 delims=:" %%G IN ('cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus ^| findstr /C:"Last 5 characters of installed product key:"') do ( set "key=%%G" )
    set "key=%key: =%"
    echo %key%