Advertisement
Rakkii

createUser

Aug 31st, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.         .Description
  3.             Script used to create a new Domain User in Active Directory.
  4. #>
  5.  
  6. #Grab some dynamic variables
  7. $firstName = Read-Host "First Name"
  8. $lastName = Read-Host "Last Name"
  9. $title = Read-Host "Title"
  10. $Dept = Read-Host "Department"
  11. $Password = Read-Host -AsSecureString "Password"
  12. $Office = Read-Host "Office"
  13. $Manager = Read-Host "Manager"
  14.  
  15.  
  16. #Set some Static variables
  17. $homepath = "\\File\Share\"
  18. $domain = '@' + (Get-ADDomain).dnsroot
  19. $lowerLN = $lastname.ToLower()
  20. $lowerFI = $firstName.substring(0,1).ToLower()
  21. $logon = $lowerFI + $lowerLN
  22.  
  23. #Set the Address Tab Info variables
  24.     if ($Office -like 'Lex Corp') {
  25.         $Street = "Insert Street Here"
  26.         $City = "Metropolis"
  27.         $State = "ZZ"
  28.         $Zip = '55555'
  29.     }
  30.     if ($Office -like 'Wayne Ent') {
  31.         $Street = "Insert Street Here"
  32.         $City = "Gotham"
  33.         $State = "ZZ"
  34.         $Zip = '77777'
  35.     }
  36.     if ($Office -like 'Scranton') {
  37.         $Street = "Insert Street Here"
  38.         $City = "Smallville"
  39.         $State = "ZZ"
  40.         $Zip = '99999'
  41.     }  
  42.    
  43. #Set a new variable to clean the script up
  44. $Attributes =@{
  45.  
  46.         Enabled = $True
  47.         Name = "$firstName  $lastName"
  48.         GivenName = $firstName
  49.         Surname = $lastName
  50.         SamAccountName = $logon
  51.         UserPrincipalName = "$logon$domain"
  52.         DisplayName = "$firstName $lastName"
  53.         Department = "$Dept"
  54.         Title = $Title
  55.         Office = $Office
  56.         Company = "Insert Company Here"
  57.         AccountPassword = $Password
  58.         HomeDrive = "H:"
  59.         HomeDirectory = "$homepath$logon"
  60.         Path = 'OU=Users,OU=$Dept,OU=Yep,DC=Contoso,DC=Com'
  61.         StreetAddress = $Street
  62.         City = $City
  63.         State = $State
  64.         PostalCode = $Zip
  65.         Country = "US"
  66.         Manager = get-aduser -Filter 'Name -like "$Manager"' -Properties SamAccountName | Select-Object -ExpandProperty SamAccountName
  67.        
  68.     }
  69. #Creating our new Domain User
  70. $mkuser = new-aduser $Attributes
  71.  
  72. #Make sure that the account has to change it's password after the first logon
  73. $ChgPass = set-aduser -identity $logon -ChangePasswordAtLogon $True
  74.  
  75. #This is where my adding to groups would go.....IF I HAD THEM!
  76.  
  77. #Check to see if account exists
  78. #$complete = get-aduser -identity $logon
  79.    
  80. #Look for the account in question, and if missing create it.
  81. If (!(Get-ADUser -Identity $logon))
  82. {
  83. & $mkuser
  84. & $ChgPass
  85. }
  86. else
  87. {
  88. Write-Host "The username already exists."
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement