Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!powershell
- import-module ActiveDirectory
- $Firstname = Read-Host -Prompt "Input the user's First Name here"
- $LastName = Read-Host -Prompt "Input the user's Last Name here"
- #user to break down what location the user is at and how with OU placment
- $loc = Read-Host -Prompt "User's Main Location"
- #New username Vaildation
- while(1) {
- $prompt = Read-Host "Input Username"
- try { $user = Get-ADUser $prompt }
- catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
- $user = $prompt
- break
- }
- "User '$prompt' already exists!"
- }
- "User to be created: $user"
- #Role To mirror - These are user accounts in AD with predefined groups.
- $clonedusername = Read-Host -Prompt "Input the username to clone group memberships from (Marketing,Accounting)"
- if ($clonedusername -eq "Marketing"){
- $OU = "OU=$Loc,OU=Sales,OU=Marketing,OU=Company Structure,DC=Contoso,DC=com"}
- ElseIf ($clonedusername -eq "Accounting"){
- $OU = "OU=$Loc,OU=Administration,OU=Accounting,OU=Company Structure,DC=Contoso,DC=com"}
- #Password Generation
- $randomgen = -join ((65..90) + (97..122) | Get-Random -Count 8 | % {[char]$_})
- #AD Fields
- $Desc = $Loc + " " + $clonedusername
- $Fullname = $Firstname + "` " + $LastName
- $samaccountname = $user.ToLower()
- $email = $samaccountname + "@Contoso.Com" #Change DOMAIN.COM to your actual domain if creating email addresses. Email address is compiled from Username + "@DOMAIN.COM"
- $password = $randomgen + "1!"
- $upn = "$samaccountname@Contoso.com" #Change DOMAIN.COM to your actual domain or respective domain for the UPN
- #Create New Users' Account
- New-ADUser -Name $Fullname -SamAccountName "$samaccountname" -GivenName "$FirstName" -Surname "$LastName" -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -DisplayName "$Fullname" -Path "$OU" -UserPrincipalName "$upn" -Title "$title" -EmailAddress "$email" -Enabled $true -Description "$desc"
- #Queries the memberof properties of the specified user to clone, Selects the memberof properties of the user to clone, Adds the newly created user to all of the same groups
- Get-ADUser -Identity $clonedusername -Properties Memberof |
- Select-Object -ExpandProperty memberof |
- Add-ADGroupMember -Members $samaccountname
- #Outputs user's details
- Write-Host *****************************
- Write-Host "Your user has been created"
- Write-Host "Full Name: $Fullname"
- Write-Host "Username: $samaccountname"
- Write-Host "Email Address: $email"
- Write-Host "The user's permissions have been copied from: $clonedusername"
- Write-Host "Password: $password"
- Write-Host "OU Path: $ou"
- Write-Host *****************************
- ##$DateStamp = get-date -uformat "%Y-%m-%d"
- ##Logging - Out-File -FilePath C:\Users\Desktop\$samaccountname-$dateStamp.log -Confirm -Encoding ascii
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement