question

IT-Texas avatar image
0 Votes"
IT-Texas asked IanXue-MSFT edited

Powershell 7 - Unjoin computer from Domain

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-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

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://docs.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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DSPatrick avatar image
0 Votes"
DSPatrick answered

Might try adding it to a workgroup.
Add-Computer -WorkgroupName WORKGROUP-A
https://docs.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--


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IT-Texas avatar image
0 Votes"
IT-Texas answered

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

93625-image.png



image.png (58.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DSPatrick avatar image
0 Votes"
DSPatrick answered

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--




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IT-Texas avatar image
0 Votes"
IT-Texas answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.