Skip to content

Instantly share code, notes, and snippets.

@kyleericson
Last active May 21, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleericson/9ccc7e7bd7f1390efbad89e110ce5c6b to your computer and use it in GitHub Desktop.
Save kyleericson/9ccc7e7bd7f1390efbad89e110ce5c6b to your computer and use it in GitHub Desktop.
AD part 2
$selected = Get-ADUser -Filter * -Properties Name, description, department, SamAccountName, distinguishedname, description, memberof | Where {$_.enabled } | select -Property Name, department, description, SamAccountName,distinguishedname, memberof| Out-GridView -passthru
$selectedsam = $selected.SamAccountName
$selecteddn= $selected.distinguishedname
$selectedgr= $selected.memberof
$selectedds = $selected.description
write-host $selectedsam
write-host $selecteddn
write-host $selectedgr
write-host $selectedds
$accLocation = $selecteddn -creplace "^[^,]*,",""
$accLocation
$new_password = 'acemfg1'
$new_password = (ConvertTo-SecureString -AsPlainText $new_password -Force);
$User = Get-AdUser $selectedsam
$DN = $User.distinguishedName
$OldUser = [ADSI]"LDAP://$DN"
$Parent = $OldUser.Parent
$OU = [ADSI]$Parent
$OUDN = $OU.distinguishedName
$firstname = Read-Host "First Name"
$Lastname = Read-Host "Last Name"
$NewUser = Read-Host "New Username"
$NewName = "$firstname $lastname"
$Description = $selected.description
$Displayname= "$firstname $lastname"
New-ADUser -SamAccountName $NewUser -Name $NewName -GivenName $firstname -displayname $Displayname -Surname $lastname -Description $Description -Instance $DN -Path "$OUDN" -Enabled $True -AccountPassword $new_password
Get-ADUser $NewUser -Properties userPrincipalName | foreach { Set-ADUser $_ -UserPrincipalName "$($_.samaccountname)@truemfg.com"}
Get-ADPrincipalGroupMembership $User | foreach { Add-ADPrincipalGroupMembership -identity $NewUser -MemberOf $_.samaccountname } -ErrorAction Ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment