I am trying to create a winform to create domain users . I am stuck on "add-adgroupmember" It's keep giving me error messgae" Add-ADGroupMember: Cannot convert 'Group1 Group2 Group3 Group4' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported." All you need to do is change the "UserPrincipalName" and the code will work on any domain.
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
Import-Module Microsoft.PowerShell.Security
$Form = New-Object System.Windows.Forms.Form
$textboxName = New-Object System.Windows.Forms.TextBox
$textboxPassword = New-Object System.Windows.Forms.TextBox
$textboxDescription = New-Object System.Windows.Forms.TextBox
$addbutton = New-Object System.Windows.Forms.Button
$droplist = New-Object System.Windows.Forms.ComboBox
$labelname = New-Object System.Windows.Forms.Label
$labelPassword = New-Object System.Windows.Forms.Label
$labelDescription = New-Object System.Windows.Forms.Label
$labelOU = New-Object System.Windows.Forms.Label
$checklist = New-Object System.Windows.Forms.CheckedListBox
$Password = New-Object System.Security.SecureString
$textboxName.Location = '23,23'
$textboxName.Size = '150,23'
$labelname.Text = 'Name:'
$labelname.Location = '23,5'
$textboxPassword.Location = '23,83'
$textboxPassword.Size = '150,23'
$labelPassword.Text ='Password:'
$labelPassword.Location = '23,65'
$textboxDescription.Location = '23,143'
$textboxDescription.Size = '150,23'
$labelDescription.Text = 'Description:'
$labelDescription.Location = '23,125'
$droplist.Location = '150,200'
$droplist.width = 240
$droplist.Height = 20
$oulist = Get-ADOrganizationalUnit -Filter 'Name -like "*"'
$droplist.Items.AddRange($oulist)
$labelOU.Text = 'OU'
$labelOU.Location = '150,180'
$checklist.Location = '185,80'
$checklist.Size = '200,100'
$checklist.CheckOnClick = $true
Get-ADGroup -Filter * |Select-Object Name | Out-File -FilePath C:\Windows\Temp\adgroup.txt
$grouplist = Get-Content C:\Windows\Temp\adgroup.txt
$checklist.Items.AddRange($grouplist)
$addbutton.Text = 'Add'
$addbutton.Location = '196,23'
$addbutton.Add_Click({
$Password = $textboxPassword.Text | ConvertTo-SecureString -AsPlainText -Force
If($textboxName.Text){New-ADUser $textboxName.Text -Enabled $true -AccountPassword $Password -ChangePasswordAtLogon $true -Description $textboxDescription.text -UserPrincipalName ($textboxName.Text+'@Matt Jemmett .lab') -Path $droplist.Text | Add-ADGroupMember -Identity $checklist.CheckedItems -Members $textboxName.Text -Confirm}
})
$Form.Controls.Add($textboxName)
$Form.Controls.Add($textboxPassword)
$Form.Controls.Add($textboxDescription)
$Form.Controls.Add($addbutton)
$Form.Controls.Add($droplist)
$Form.Controls.Add($labelname)
$Form.Controls.Add($labelPassword)
$Form.Controls.Add($labelDescription)
$Form.Controls.Add($labelOU)
$Form.Controls.Add($checklist)
$Form.ShowDialog()