title: Issue Adding PC to domain and changing name at the same time
body: <p>I am trying to simplify our process to add a PC to a domain.  So far I can change a PC name, restart and  add the PC to a specific OU and another restart.</p>
  
  <p>I have the same problem as the OP in <a href="https://stackoverflow.com/questions/6217799/rename-computer-and-join-to-domain-in-one-step-with-powershell">this topic</a>, and I have tried to follow the suggestions yet I am getting an error.</p>
  
  <p>I am referring to the target computername yet it says the parameter could not be found even though I set it earlier in the script. </p>
  
  <blockquote>
    <p>Error "Add-Computer : A parameter cannot be found that matches parameter name 'computername'. At C:\RenameComputer.ps1:10 char:47"</p>
  </blockquote>
  
  <p>My code is as follows:</p>
  
  <pre><code>$computername = get-wmiobject win32_computersystem
  $computername
  $name = read-host -Prompt "Please enter computer name you want to use:"
  $computername.rename($name)
  $domain = "Domain"
  $username = read-host -Prompt "Please enter Admin Account - domain\id:"
  $password = read-host -Prompt "Please enter Admin Password" -AsSecureString
  $credential = New-Object System.Management.Automation.PSCredential($username,$password)
  Add-Computer -DomainName $domain -computername $computername -newname $name -Credential $credential -OUPath "OU=Sub Container,OU=Parent Container,DC=Domain,DC=com"
  </code></pre>
  
  <p>Also, if the PC exists in OU, or was not deleted from the domain before a PC rebuild, if I add the new computer to a domain, then rename it to an object that is already there, I would expect it to throw an error, is there anyway to avoid that?</p>
  
  <p>LATEST </p>
  
  <p>Many thanks for your reply 
  After testing the PS2 script provided below, and amending it to suit our OU's, I got this error</p>
  
  <pre><code>Get-WmiObject : Invalid query
  At C:\RenameComputer.ps1:8 char:26
  + $computer = Get-WmiObject &lt;&lt;&lt;&lt;  -Class Win32_ComputerSystem `
      + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], Managemen
     tException
      + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
     ommands.GetWmiObjectCommand
  
  You cannot call a method on a null-valued expression.
  At C:\RenameComputer.ps1:12 char:32
  + $computer.JoinDomainOrWorkGroup &lt;&lt;&lt;&lt; ($domain, $password, $username, $ou, 3,
  $true)
      + CategoryInfo          : InvalidOperation: (JoinDomainOrWorkGroup:String)
      [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  You cannot call a method on a null-valued expression.
  At C:\RenameComputer.ps1:13 char:17
  + $computer.Rename &lt;&lt;&lt;&lt; ($newname)
      + CategoryInfo          : InvalidOperation: (Rename:String) [], RuntimeExc
     eption
      + FullyQualifiedErrorId : InvokeMethodOnNull
  </code></pre>
  
  <hr>
  
  <p>I then amended the code $computer = Get-WmiObject code to read</p>
  
  <pre><code>$domain = "Domain"
  $ou     = "OU=Sub Container,OU=Parent,DC=Domain,DC=com"
  
  $newname  = read-host -Prompt "Please enter computer name you want to use:"
  $username = read-host -Prompt "Please enter Admin Account - domain\id:"
  $password = read-host -Prompt "Please enter Admin Password"
  
  $computer = Get-WmiObject Win32_ComputerSystem
  $computer = $computer.name
  
  $computer.JoinDomainOrWorkGroup($domain, $password, $username, $ou, 3, $true)
  $computer.Rename($newname)
  
  #Restart-Computer
  </code></pre>
  
  <p>and I get this error</p>
  
  <pre><code>Method invocation failed because [System.String] doesn't contain a method named
   'JoinDomainOrWorkGroup'.
  At C:\RenameComputer.ps1:11 char:32
  + $computer.JoinDomainOrWorkGroup &lt;&lt;&lt;&lt; ($domain, $password, $username, $ou, 3,
  $true)
      + CategoryInfo          : InvalidOperation: (JoinDomainOrWorkGroup:String)
      [], RuntimeException
      + FullyQualifiedErrorId : MethodNotFound
  
  Method invocation failed because [System.String] doesn't contain a method named
   'Rename'.
  At C:\RenameComputer.ps1:12 char:17
  + $computer.Rename &lt;&lt;&lt;&lt; ($newname)
      + CategoryInfo          : InvalidOperation: (Rename:String) [], RuntimeExc
     eption
      + FullyQualifiedErrorId : MethodNotFound
  </code></pre>
  
  <p>Should I not be using the Add-Computer method at this point?</p>