'Get-Credentials error in PowerShell 'Cannot find an Overload for "PSCredential"'

I'm new to PowerShell. I am trying to make it so I can setup a new computer connecting to the network to allow me to do certain tasks. When I run this:

$domain = "mydomain.com" 
$mycred = get-credential
$credential = New-Object System.Management.Automation.PSCredential("$($domain)\$($mycred.Username)","$($mycred.Password)")
$compName = Read-Host -Prompt "Enter new computer name"
Add-Computer -DomainName $domain -newname $compName -Credential $credential -Restart
Pause

I get the error:

New-Object : Cannot find an overload for "PSCredential" and the argument count: "2". At C:\Users\entername\Downloads\1-JoinDomainCred.ps1:7 char:15

  • ... redential = New-Object System.Management.Automation.PSCredential("$($ ...
  • CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
  • FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Where am I going wrong?



Solution 1:[1]

Get-Credential aready returns a proper credentials object. Just use that:

 $mycred = Get-Credential; Add-Computer ... -Credential $mycred

PowerShell is not C#, pass the arguments as an array without the ():

$credential = New-Object System.Management.Automation.PSCredential "$($domain)\$($mycred.Username)",$mycred.Password

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven Marks