Unable to run dsregcmd command from PowerShell when deploy to client machines via SCCM package.

Prabhjot Singh 165 Reputation points
2024-05-15T13:11:12.2666667+00:00

Hi Team,

we are facing issue, just want to assure the device status whether it is Entra joined or not using "dsregcmd" command line. We created a script to check the device status, then created a package in SCCM to deploy this script which includes dsregcmd /status command and distributed the content and deployed to client machines.

We received the content in Software center in device, after we execute the script. we are facing issue-

The term '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 +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : ObjectNotFound: (C:\Windows\System32\dsregcmd.exe:String) [], CommandNotFoundException     + FullyQualifiedErrorId : CommandNotFoundException & : The term '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 +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : ObjectNotFound: (C:\Windows\System32\dsregcmd.exe:String) [], CommandNotFoundException     + FullyQualifiedErrorId : CommandNotFoundException

Command-line we associated in package is-

"%Windir%\system32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy ByPass -file "Dsregcmd.ps1"

Is there any line we need to modify or add if missing? Any permissions related issues. As the dsregcmd path is specified - C:\Windows\System32\dsregcmd.exe

We have perform all permutations and combinations to get fix this. Can anyone help on this to us.

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,489 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,184 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. glebgreenspan 1,370 Reputation points
    2024-05-15T14:21:57.77+00:00

    Hello Prabhjoy

    The issue you're facing is due to the way PowerShell is trying to execute the dsregcmd command. The problem is that the script is trying to run dsregcmd as a PowerShell command, which is not correct. dsregcmd is a standalone executable, not a PowerShell command.

    To fix this, you can modify your PowerShell script to simply execute the dsregcmd command as a separate process, like this:

    & "C:\Windows\System32\dsregcmd.exe" /status

    This will execute the dsregcmd command and capture its output.

    Alternatively, you can modify your package's command line to run the PowerShell script without trying to execute dsregcmd as a PowerShell command. You can do this by changing the command line to:

    "%Windir%\system32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy ByPass -file "C:\Path\To\Dsregcmd.ps1"

    This will run the Dsregcmd.ps1 script as a separate PowerShell process, which should execute the dsregcmd command correctly.

    As for permissions-related issues, make sure that the account running the script has the necessary permissions to execute the dsregcmd command. The dsregcmd command requires elevated privileges to run, so you may need to add the necessary permissions or run the script as an administrator.

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


  2. AllenLiu-MSFT 41,451 Reputation points Microsoft Vendor
    2024-05-16T06:07:44.6033333+00:00

    Hi, @Prabhjot Singh

    Thank you for posting in Microsoft Q&A forum.

    You may try use the command line in package:

    "%Windir%\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -Command .\Dsregcmd.ps1
    

    For your reference:

    https://www.sccmog.com/deploy-powershell-script/


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Add comment".