Hello
It seems like you're trying to run the dsregcmd
command from PowerShell, but it's not recognized as an external or internal command. This is because the dsregcmd
command is a part of the Windows operating system, and it's not included in the PowerShell's default path.
Option 1: Use the full path to the dsregcmd command
You can modify your script to use the full path to the dsregcmd
command, like this:
$dsregOutput = Invoke-Expression -Command "C:\Windows\System32\dsregcmd.exe /status"
This will ensure that PowerShell finds the correct executable and runs it.
Option 2: Set the system path to include the Windows System32 directory
You can add the Windows System32 directory to the system path using the following command:
[System.Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Windows\System32", [System.EnvironmentVariableTarget]::Machine)
his will add the Windows System32 directory to the system path, allowing you to run dsregcmd
from any directory.
Option 3: Run PowerShell as an administrator
If you're running PowerShell as a non-admin user, it might not have access to the dsregcmd
command. Try running PowerShell as an administrator, and see if that resolves the issue.
Additional suggestion:
When deploying scripts using SCCM, it's a good practice to use the Start-Process
cmdlet to run external commands, like dsregcmd
. This will ensure that the command is executed in a separate process, and it will also help with any potential issues related to PowerShell's execution policy.
Here's an updated version of your script:
Start-Transcript -Path "$PSScriptRoot\unreg.log"
$dsregOutput = Start-Process -FilePath "C:\Windows\System32\dsregcmd.exe" -ArgumentList "/status" -Wait -PassThru
$azureADJoinStatus = ($dsregOutput.StandardOutput | Select-String -Pattern "AzureAdJoined").Line.Split(":")[1].Trim()
... rest of your script ...
This should help you run the dsregcmd
command successfully from PowerShell.