Joshua German
Joshua German

Reputation: 1

How do I use Cmdkey.exe /list powershell script to report on microsoft account licenses within Office apps?

We're trying to only report on the following information after running this powershell script.

"LegacyGeneric:target:MicrosoftAccount:user="

cmdkey.exe /list | ForEach-Object {$found=$false} {
    $line = $_.Trim()
    if ($line -eq '') 
    {
        if ($found) { $newobject }
        $found = $false
        $newobject = '' | Select-Object -Property Type, User, Info, Target
    }
    else
    {
        if ($line.StartsWith("Target: "))
        {
            $found = $true
            $newobject.Target = $line.Substring(8)
        }
        elseif ($line.StartsWith("Type: "))
        {
            $newobject.Type = $line.Substring(6)
        }
        elseif ($line.StartsWith("User: "))
        {
            $newobject.User = $line.Substring(6)
        }
        else
        {
            $newobject.Info = $line
        }

    }
}

The idea behind is it to only get the Microsoft account that Microsoft Office applications are using to obtain their licenses, shown here.

Credential Manager example

Thanks in advance to anyone willing to help out!!

Upvotes: 0

Views: 1295

Answers (1)

Doug Maurer
Doug Maurer

Reputation: 8878

Not sure if ours are different or if you have a typo. You list LegacyGeneric:target:MicrosoftAccount:user= but I only see LegacyGeneric:target=MicrosoftAccount:user=

Please modify it if mine is just different. If you are just wanting the account, you could use something like this.

cmdkey /list | foreach {
    if($_ -match 'LegacyGeneric:target=MicrosoftAccount:user=(.*)')
    {
        [PSCustomObject]@{Account = $matches.1}
    }
}

Output for me looked like

Account            
-------            
[email protected]
[email protected]

If you wanted a one-liner it could do that too.

[PSCustomObject]@{Account = $(cmdkey /list | foreach {if($_ -match 'LegacyGeneric:target=MicrosoftAccount:user=(.*)'){$matches.1}})}

Upvotes: 1

Related Questions