Install MAK product key for Office 2013 and activate it

# Requires -Version 2.0

Function Set-O2013Key {
[CmdletBinding()]
Param(
    [parameter(Mandatory=$true)]
    [ValidatePattern('(([0-9A-Z]{5}\-)){4}([0-9A-Z]{5})$')]    
    [system.string]$Key=$null
)
Begin {
    # Make sure we run as admin                                    
    $usercontext = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()                                    
    $IsAdmin = $usercontext.IsInRole(544)                                                       
    if (-not($IsAdmin)) {                                    
        Write-Warning "Must run powerShell as Administrator to perform these actions"                                    
        break
    }              
    Function Test-isWin7OS {
    param()
        Begin {
            $isWin7 = $false
        }
        Process {
            try {
               $OS = Get-WmiObject -Query "Select * from Win32_OperatingSystem" -ErrorAction Stop
            } catch {
                $isWin7 = $false
            }
            if ($OS) {
                $Version = $OS.Version -split ".",-1,1
                if ($Version[0] -eq 6 -and $Version[1] -eq 1 -and ($OS.ProductType -eq 1 -or $OS.ProductType -eq 2 -or $OS.ProductType -eq 3)) {
                    $isWin7 = $true
                }
            }
            return $isWin7
        } 
        End {}
    }
    if (Test-isWin7OS) {
        # We run on W7, verify the osppsvc service
        try {
            Get-Service -Name osppsvc -ErrorAction Stop | Out-Null
        } catch {
            Write-Warning "Error: The Software Protection Platform service is not installed."
            break
        }
        $Class = 'OfficeSoftwareProtectionProduct'
        $SLsvc = Get-WmiObject -Class OfficeSoftwareProtectionService
    } else {
        # Win8 and beyond
        $Class = 'SoftwareLicensingProduct'
        $SLsvc = Get-WmiObject -Class SoftwareLicensingService
    }
} 
Process {
    # Install MAK prodcut key: /inpkey
    try {
        Invoke-WmiMethod -InputObject $SLsvc -Name InstallProductKey -ArgumentList $Key -ErrorAction Stop | Out-Null
    } catch {
        Write-Warning -Message "Failed to install MAK for Office 2013"
    }
    # Activate: /act
    Get-WmiObject -Query "Select * FROM $Class WHERE PartialProductKey <> null" | 
    Where { $_.ApplicationID -eq "0ff1ce15-a989-479d-af46-f275c6370663"} | ForEach-Object -Process {
        try {
            Invoke-WmiMethod -InputObject $_ -Name Activate -ErrorAction Stop | Out-Null
            Write-Verbose -Message "Successfully activated $($_.Name)"
        } catch {
            # Silently fail
        }
    }
}
End {}
}

Here’s how to use it:

Set-O2013Key -Key '12345-12345-12345-12345-12345' -Verbose            

All I did is read the vbscript responsible for activating Office 2013 that you can find under %programfiles%\Microsoft Office\Office15\OSPP.VBS

About these ads

5 thoughts on “Install MAK product key for Office 2013 and activate it

  1. So what did you do? I accessed the file by placing .txt after it and searched for 0213Key. It doesn’t exist. What do I do with the once I am in it reading it? I created a batch file .bat with the entries you listed above. All seemed to run fine with no errors but it seems to have done nothing. What does this mean please?: Set-O2013Key -Key ‘12345-12345-12345-12345-12345′ -Verbose

    • The above (powershell) code should be placed in a file with .ps1 extension.
      You should replace the sample key above by the MAK (multiple activation key) volume licensing key you’ve got from Microsoft for Office 2013.
      Then on a workstation with Office 2013 installed, you create .bat file and call the powershell script (.ps1 file) inside the batch file like this:

      "%systemroot%\system32\WindowsPowershell\v1.0\powerShell.exe" -Executionpolicy Bypass -File "%_WHEREAMI%Tools\my-powershell-scriptname.ps1"

  2. Thank you SO MUCH for writing this. I was trying to use powershell to manually run the ospp script with very little luck. Awesome post! I removed the encapsulating function to suit my needs, and it worked flawlessly!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s