How to get Product ID regardless of the type of key in PowerShell?

Timothy Bryant 0 Reputation points
2023-02-24T16:17:02.3333333+00:00

I'm trying to figure out how to get the information from all of our machines using Powershell to import into AutoPilot.

I think I've got most of it working, but the commands I have found using Powershell do not work as expected when pulling the Product ID. We have a mix of the different types of keys, so I'm wondering if that is it. If I use the command (Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey I get the following:

Get-WmiObject : A positional parameter cannot be found that accepts argument '*'.

I can use the command line and pull that info, but how do I get it into the variable?

Get system information

$serialNumber = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber

$productId = wmic path softwarelicensingservice get OA3xOriginalProductKey

$hardwareHash = Get-WmiObject -Class "Win32_ComputerSystemProduct" | Select-Object -ExpandProperty UUID

$model = Get-WmiObject -Class "Win32_ComputerSystem" | Select-Object -ExpandProperty Model

Define output file path

$outputFile = "\SERVER\output.csv"

Create output file if it doesn't exist

if (-not (Test-Path $outputFile)) {

New-Item -Path $outputFile -ItemType File

}

Append system information to output file

$data = [pscustomobject]@{

SerialNumber = $serialNumber

ProductId = $productId

HardwareHash = $hardwareHash

Model = $model

}

$data | Export-Csv -Path $outputFile -Append -NoTypeInformation


Microsoft Security | Windows Autopilot
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,396 Reputation points
    2023-02-24T17:45:51.7933333+00:00

    Your query works for me on Win 11 using PS 5.1.

    Maybe a typo somewhere?

    User's image

    Here, try this.

    # Get system information
    $serialNumber = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber
    $productId = (Get-WmiObject -query "select * from SoftwareLicensingService").OA3xOriginalProductKey
    $hardwareHash = Get-WmiObject -Class "Win32_ComputerSystemProduct" | Select-Object -ExpandProperty UUID
    $model = Get-WmiObject -Class "Win32_ComputerSystem" | Select-Object -ExpandProperty Model
    
    #Define output file path
    $outputFile = "C:\temp\test.csv"    # "
    
    # Not needed. Export-csv will create the file if its not there. 
    #Create output file if it doesn't exist
    #if (-not (Test-Path $outputFile)) {
    #    New-Item -Path $outputFile -ItemType File
    #}
    
    # Append system information to output file
    $data = [pscustomobject]@{
        SerialNumber = $serialNumber
        ProductId = $productId
        HardwareHash = $hardwareHash
        Model = $model
    }
    
    $data | Export-Csv -Path $outputFile -Append -NoTypeInformation
    
    2 people found this answer helpful.

  2. abbodi86 4,036 Reputation points
    2023-03-02T15:19:26.2966667+00:00

    OA3xOriginalProductKey only retrieve MSDM OEM key, to get the installed key regardless type:

    Get-WmiObject -query "Select * FROM SoftwareLicensingProduct WHERE ApplicationID='55c92734-d682-4d71-983e-d6ec3f16059f' AND PartialProductKey IS NOT NULL"

    then you can filter and select a property (e.g. ID, PartialProductKey, or ProductKeyID)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.