Powershell 7 - Unjoin computer from Domain

IT-Texas 41 Reputation points
2021-05-04T20:45:39.667+00:00

Hello, I migrated from PS 5.1 to 7.1 and one of the old scripts is not running. The issue is that the remove-computer cmdlet is no longer available in PS7
I need to unjoin a system from a domain but I can't find any cmdlet that will allow me to do that.
I can use remove-adcomputer to remove the computer from the DC , but that does not unjoin the system.

Can somebody point me in the right direction ?

Thanks

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
11,748 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,282 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 27,926 Reputation points Microsoft Vendor
    2021-05-05T02:58:35.92+00:00

    Hi,

    Add-Computer is only available in Windows PowerShell, however, you can try importing it using the UseWindowsPowerShell parameter in PowerShell 7.

    Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShell  
    

    When the first module is imported using Windows PowerShell Compatibility feature, PowerShell creates a remote session named WinPSCompatSession that is running in a background Windows PowerShell 5.1 process. This process is created when the Compatibility feature imports the first module.

    https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/About/about_windows_powershell_compatibility

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Dave Patrick 426.1K Reputation points MVP
    2021-05-04T21:05:55.74+00:00

    Might try adding it to a workgroup.
    Add-Computer -WorkgroupName WORKGROUP-A
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-computer?view=powershell-5.1#example-2--add-a-local-computer-to-a-workgroup

    --please don't forget to Accept as answer if the reply is helpful--

    0 comments No comments

  2. IT-Texas 41 Reputation points
    2021-05-04T21:11:11.423+00:00

    Well, unless my PS v7 is corrupted, the add-computer command is also not available

    93625-image.png

    0 comments No comments

  3. Dave Patrick 426.1K Reputation points MVP
    2021-05-04T22:45:28.18+00:00

    This worked for me.

    PS C:\Users\administrator> Add-Computer -ComputerName win7x64 -Credential domain\administrator -WorkgroupName WORKGROUP-A

    Confirm
    After you leave the domain, you will need to know the password of the local Administrator account to log onto this
    computer. Do you wish to continue?
    [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y
    WARNING: The changes will take effect after you restart the computer win7x64.
    PS C:\Users\administrator>

    --please don't forget to Accept as answer if the reply is helpful--

    0 comments No comments

  4. IT-Texas 41 Reputation points
    2021-05-05T01:32:33.057+00:00

    Thanks for your answer. I can make my scripts work under PowerShell 5, but my original question was about unjoining a system from a domain using PS version 7.
    I modified my systems to run any ps1 files under PS7 so I would like to find a way to unjoin ( and eventually re-join) a system to a DC using PS7

    Thanks

    0 comments No comments