Search code examples
powershelloffice365registry

How do get O365 login status from Registry?


I want to get a certain registry key name underneath HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities

As per the screenshot below, I do have two sub-keys, and I like to have the sub-key name for the alphanumeric one with the _ADAL at the end of the name.

Inside this key, there would be a property "SignedOut", when my user is not logged-in to Office365. This is the information that I need to spit out a message box.

enter image description here

I was first looking into the MSOline cmdlet, but this will be deprecated in March 2024, so no option anymmore. GraphAPI will be too complex since I have to register my script to get authentication to the API according to company policy.

Now, we are looking into registry keys, which seem to host the information that I need:

When I exclude the "_AD" at the end of the 2nd key, I get all the ChildItems of my desired key. My issue is, that the returned "PSCustomObject" contains the name, the properties and also the corresponding values.

Get-ChildItem -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities' -Exclude *_AD

How can I check whether a "SignedOut" property does exist for this registry key with the alphanumberic name?


Solution

  • Use Get-ItemProperty, and assuming that only one key matches *_ADAL:

    $prop = 
      Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities\*_ADAL' -Name SignedOut
    
    • If the property (registry value) doesn't exist, $prop will be $null, so you can use $null -eq $prop to determine whether it exists.

    • If it does exist and you want to extract the property value from the [pscustomobject] instance returned, use $prop.SignedOut


    If there's a chance that multiple keys match and / or you want to know the exact name of the matching key(s), you can collect their names and SignedOut values in custom objects as follows, using Select-Object and calculated properties:

    Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities\*_ADAL' -Name SignedOut | 
      Select-Object @{ n='Identity'; e='PSChildName' }, 
                    @{ n='SignedOut'; e={ $_.SignedOut } }