Formatting Output of dsregcmd /status

Hi team,

Any suggestion on how to convert the output of dsregcmd /status into an object? Or, any PowerShell alternative to find if a machine has joined AzureAd or not.

I am getting an output something like this:

PS C:> dsregcmd /status

±---------------------------------------------------------------------+
| Device State |
±---------------------------------------------------------------------+

    AzureAdJoined : YES
 EnterpriseJoined : NO</strong>

I have to further put an if condition using AzureAdJoined : YES parameter, but I am not able to select it, as it is not a property.
Any help on filtering or formatting?

Thanks,

If you have only the “strings” from the command you have to parse it by yourself. As far as I know there is no built in magic what transforms this to a Powershell object. So catch the text and use regex to cut it into suitable pieces.

Look at ConvertFrom-String as a possible solution

https://cdn-powershell.pressidium.com/wp-content/uploads/2018/02/Iron-Scripter-Prequel-Puzzle-4-A-commentary.pdf has some good examples of using “ConvertFrom-String”

Hello,

I made a little a function with help from the link that Jon posted. Thanks for the link, was a good read.

function Get-DSregcmdstatus {
    
    $status = dsregcmd /status 
    $status -replace ':', ' ' | 
        Select-Object -Index 5, 6, 7, 13, 14, 15, 16, 17, 18, 19, 25, 26, 27, 28, 29, 30, 31 | 
        ForEach-Object {$_.Trim() }  | 
        ConvertFrom-String -PropertyNames 'State', 'Status'
} 

Check it out! I had to do 3 examples.

$template = @'
        AzureAdJoined : {AzureAdJoined*:YES}
     EnterpriseJoined : {EnterpriseJoined:NO}
        AzureAdJoined : {AzureAdJoined*:NO}
     EnterpriseJoined : {EnterpriseJoined:YES}
        AzureAdJoined : {AzureAdJoined*:NO}
     EnterpriseJoined : {EnterpriseJoined:NO}
'@

PS C:\> dsregcmd /status | ConvertFrom-String -TemplateContent $template

AzureAdJoined EnterpriseJoined
------------- ----------------
NO            NO

Thanks Richard, that helped. One question, when I used the code:

dsregcmd /status | ConvertFrom-String | where P2 -eq 'AzureAdJoined'

It’s generating properties as P1, P2, P3 and P4. Just wanted to understand if they are random or consistent so that I can hard-code it to my script?

Thanks Anders, That was help. I found another solution to it meanwhile :). I know it’s not a formatting solution, however it worked for a temporary break-fix.

$status = dsregcmd /status | Select-String -Pattern "AzureAdJoined"

    if ($status -match "AzureAdJoined : YES")
        {
            [System.Windows.MessageBox]::Show('Your computer is already a member of AzureAd...') 
        }
    
    else
    {

Thanks Jon, It was a good read. Got multiple solutions for one scenario now… :slight_smile:

Great JS, thanks a lot. I can see multiple solutions now… Happiest I am… :slight_smile: