MostafaSalaheldien-7490 avatar image
0 Votes"
MostafaSalaheldien-7490 asked XingyuZhao-MSFT commented

How to join and unjoin a domain using poweshell in

Hello everyone,
I would like to join a domain using I want to insert this line in PowerShell after I launch it
I am using this code

' Process.Start("Powershell", "add-computer -domainname xxx -credential AD\xxx -restart -force" )

it's working for me but not asking to enter the username and the password for the domain

I found another code

     Process.Start("powershell", "$domain = "MYDOMAIN.COM" + _

"$password = Read-Host -Prompt "Enter password For $user" -AsSecureString" +
"$credential = New-Object System.Management.Automation.PSCredential($username,$password)" +

"Add-Computer - DomainName $domain -Credential $credential"")

but it's not working at all
thanks in advanced
anonymous user

· 1
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.

Hi @MostafaSalaheldien-7490 ,
You can try the following code to see if it works for you.

     Process.Start("powershell", "$domain = 'MYDOMAIN.COM';" +
             "$password = Read-Host -Prompt 'Enter password For $user' -AsSecureString;" +
             "$credential = New-Object System.Management.Automation.PSCredential($username,$password);" +
             "Add-Computer - DomainName $domain -Credential $credential;")
1 Vote 1 ·

0 Answers