Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Remove License Key for Office 365

Hi all,

I am trying to edit a PowerShell working here to retrieve the installed product key for an Office 365 installation so I can then take that output and uninstall the key with ospp.vbs. But I cannot get the pipe to catch the output of the key and run it to uninstall it.

Any ideas?

This link has a code: https://community.spiceworks.com/how_to/48973-remove-and-re-add-license-key-for-office-2016-on-office-365

Here is the code:

#store the license info into an array
$license = cscript ‘C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS’ /dstatus
#license name from /dstatus
$o365 = “OfficeO365ProPlusR_Subscription1 edition”
#loop till the end of the array searching for the $o365 string 
for ($i=0; $i -lt $license.Length; $i++){
if ($license[$i] -match $o365){
$i += 6
#jumping six lines to get to the product key line in the array
$keyline = $license[$i]
#extra step
$prodkey = $keyline.substring($keyline.length – 5, 5)
#getting the last 5 characters of the line (prodkey)
} 
}
#removing the key from the workstation 
cscript ‘C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS’ /unpkey:$prodkey

Testing it on computer it runs but ends with: Unsupported command passed. A value is required for: /unpkey which means that the gathered data does not work putting it into $prodkey

Update: Import file can be downloaded in the bottom of the post.

Regards, Timmel

0

Comments

5 comments
Date Votes
  • It looks like the script on Spiceworks had weird quote characters. Try this:

    #store the license info into an array
    $license = cscript 'C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS' /dstatus
    #license name from /dstatus
    $o365 = "OfficeO365ProPlusR_Subscription1 edition"
    #loop till the end of the array searching for the $o365 string 
    for ($i = 0; $i -lt $license.Length; $i++) {
        if ($license[$i] -match $o365) {
            $i += 6
            #jumping six lines to get to the product key line in the array
            $keyline = $license[$i]
            #extra step
            $prodkey = $keyline.substring($keyline.length – 5, 5)
            #getting the last 5 characters of the line (prodkey)
        } 
    }
    #removing the key from the workstation 
    cscript 'C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS' /unpkey:$prodkey
    
    0
  • Hi Colby, I figured it out!

    Seems that it is not longer called OfficeO365ProPlusR_Subscription1 edition but Office16O365ProPlusR_Subscription1 edition. And lastly it needs to jump 7 rows.

    Here is the correct code.

    #store the license info into an array
    $license = cscript 'C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS' /dstatus
    #license name from /dstatus
    $o365 = "Office16O365ProPlusR_Subscription1 edition"
    #loop till the end of the array searching for the $o365 string 
    for ($i = 0; $i -lt $license.Length; $i++) {
        if ($license[$i] -match $o365) {
            $i += 7
            #jumping seven lines to get to the product key line in the array
            $keyline = $license[$i]
            #extra step
            $prodkey = $keyline.substring($keyline.length – 5, 5)
            #getting the last 5 characters of the line (prodkey)
        } 
    }
    #removing the key from the workstation 
    cscript 'C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS' /unpkey:$prodkey
    

    End result was:

    Uninstalling product key for: Office 16, Office16O365ProPlusR_Subscription1 edition

    If someone wants the code and import it into PDQ Inventory as a Tool, I have sent it to support@pdq.com. Maybe they can attach it in the post? Doesn't look like I have that option...

    Regards, Martin

    0
  • Awesome, I'm glad you were able to figure it out! Unfortunately, the only files that can be attached to posts are images. If you want to share the XML file of the Tool you created, I recommend uploading it to something like Pastebin or gist.github.com.

    0
  • If intereted file can be downloaded here: Reset Office 365 Key on Client

    0
  • The code above and others does not recognize the fact that different versions of Office will have different identifiers from the "cscript" product ID matching... Enterprise O365 versus Retail install of Office 2016/2019... Consumer install of O365 Personal, etc.. 

    If you need to uninstall "ALL OFFICE LICENSING" from a PC ie. Nuking Office installations and wiping all licensing from the PC, use the below code which ONLY looks for the Product Key and sends a cscript to uninstall the products one by one. 

    This below worked for me where as all others did not:

     

    $license = cscript 'C:\Program Files\Microsoft Office\Office16\OSPP.VBS' /dstatus
    $o365 = "Last 5 characters of installed product key:"
    for ($i = 0; $i -lt $license.Length; $i++) {
    $len = $license[$i].length
    if ($license[$i] -match $o365) {
    $prodkey = $license[$i].substring($len – 5, 5)
    if ($prodkey.length -eq 5) {
    cscript 'C:\Program Files\Microsoft Office\Office16\OSPP.VBS' /unpkey:$prodkey
    }
    }
    }
    0