Powershell script for joing a domain instead of workgroup

$domain = "test.local"
$password = "@TestPass" | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\test.domain02" 
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential

wrong username or password is error that showing me, maybe because that @ in the password? Because when i tried with different password it works.
When I tried to add it manualy it works.

Use single qoutes for literals and double if you want to expand variables, etc.:

$domain = 'test.local'
$password = '@TestPass' | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\test.domain02" 
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential

Personally, to avoid Powershell trying to expand or resolve something I do not want, I use format string to building strings and concentanation:

$username = '{0}\test.domain02' -f $domain
1 Like

I tried like this and worked

Start-Process powershell -verb runas -ArgumentList "-file C:\domain.ps1"


$domain = "test.local"
$password = "@m`$pas#2hj" | ConvertTo-SecureString -asPlainText -Force
$username = '{0}\test.domain02' -f $domain
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential

and it worked, but something is wrong, when i run with powershell that script, it just open a infinity times powershells sreens like this

this two screens and goes from up to down infinity times, why is that?
when i reboot, i can see that pc is in domain and thats all ok, but why that screens goes wild :smiley: i can add a script for reboot PC at the end of the script, but cant figure out why the screens just goes like that