DSREGCMD not running from powershell

Anant Bera 151 Reputation points
2024-05-15T13:07:18.8766667+00:00

Hi Experts

We are trying to run a PowerShell script from sccm to unregister the device from azure hybrid join for the Windows 11 devices. we have tried to run the script from sccm deployment as system object as well as from user rights but we are getting the error in both cases

The execution policy is unrestricted

the script is "Start-Transcript -Path "$PSScriptRoot\unreg.log"

Get the output of dsregcmd /status

$statusOutput = & "$env:SystemRoot\System32\dsregcmd.exe" /status

Check if the Azure AD join status is "Yes"

if ($statusOutput -match "AzureAdJoined\s+:\s+Yes") {

Write-Host "Azure AD joined. Initiating leave process..."

# Run dsregcmd /leave to leave Azure AD

& "$env:SystemRoot\System32\dsregcmd.exe" /leave

Write-Host "Left Azure AD successfully."

} else {

Write-Host "Azure AD not joined."

}

Stop-Transcript"

and the error that we are currently getting is "C:\Windows\System32\dsregcmd.exe' is not recognized as the name of a cmdlet, function, script file, or  operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try  again. At C:\Windows\ccmcache\18\runad.ps1:5 char:19 + $statusOutput = & "$env:SystemRoot\System32\dsregcmd.exe" /status +                  "

we are getting two common error

the path or the dsregcmd is not recognized as external or internal command

  • are there any other alternative to do the same without using the dsregcmd
  • what is the best way to run the dsregcmd from powershell
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,835 questions
Microsoft Configuration Manager
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,570 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. glebgreenspan 2,235 Reputation points
    2024-05-15T14:24:49.2533333+00:00

    Hello Anant

    The issue is that the PowerShell script is trying to execute dsregcmd as a PowerShell command, which is not correct. dsregcmd is a standalone executable, not a PowerShell command.

    To fix this, you can try the following:

    1. Use the Start-Process cmdlet to run dsregcmd as a separate process:

    $process = Start-Process -FilePath "$env:SystemRoot\System32\dsregcmd.exe" -ArgumentList @("/status") -Wait

    $statusOutput = $process.StandardOutput.ReadToEnd()

    This will execute dsregcmd and capture its output as a string.

    1. Use the & operator to run dsregcmd as a command, but specify the full path to the executable:

    $statusOutput = & "$env:SystemRoot\System32\dsregcmd.exe" /status

    This should work as long as the dsregcmd executable is in the system32 directory.

    As for alternatives to using dsregcmd, you can try using the Get-ADComputer cmdlet from the Active Directory PowerShell module to check the Azure AD join status:

    import-module -name activedirectory

    $computer = Get-ADComputer -Identity $env:COMPUTERNAME

    if ($computer.AzuresDeviceJoined) {

    # Run dsregcmd /leave to leave Azure AD
    
    & "$env:SystemRoot\System32\dsregcmd.exe" /leave
    

    }

    This will require installing the Active Directory PowerShell module on your target machines.

    As for running dsregcmd from PowerShell, you can try using the following commands:

    & "$env:SystemRoot\System32\dsregcmd.exe" /status

    & "$env:SystemRoot\System32\dsregcmd.exe" /leave

    These commands should work as long as the dsregcmd executable is in the system32 directory.

    I hope this helps! Let me know if you have any further questions.

    0 comments No comments

  2. Ian Xue 37,536 Reputation points Microsoft Vendor
    2024-05-16T01:21:33.49+00:00

    Hi Anant Bera,

    Thanks for your post.

    It seems dsregcmd.exe cannot be found in C:\Windows\System32.

    Please open PowerShell as administrator and run sfc /scannow to repair the missing file.

    https://support.microsoft.com/en-us/topic/use-the-system-file-checker-tool-to-repair-missing-or-corrupted-system-files-79aa86cb-ca52-166a-92a3-966e85d4094e

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    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.