Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- param (
- $Path = "C:\Users\Administrator\Desktop\users.csv",
- $Domain = "a18zagbi.local",
- $Password = "Syp9393"
- ) # params
- # Import Active Directory module
- Import-Module ActiveDirectory
- try {
- $users = Import-Csv -Encoding UTF7 -Delimiter ";" -Path $Path
- }
- catch {
- Write-Error $_
- } # try catch file
- $defpassword = (convertto-securestring $Password -AsPlainText -Force)
- # Create users
- foreach ($user in $users) {
- $ADUser = $null
- $domain = $Domain
- $name = $user.Name
- $login = $user.Login
- # $department = $user.Department
- $email = $user.Email
- $description = $user.Description
- If (Get-AdUser -F {(SamAccountName -eq $login) -and (enabled -eq $true)}) {
- Write-Output "there following user exists $login and is enabled"
- # should break
- }
- elseif (Get-AdUser -F {(SamAccountName -eq $login) -and (enabled -eq $False)}) {
- Write-Output "Please enable the following user $login"
- # should break
- } # elseif
- # Departments OU
- switch ($user.Department) {
- "RND" { $OUPath = "OU=RND,DC=a18zagbi,DC=local" } # Modify with the correct path
- "IT" { $OUPath = "OU=IT,DC=a18zagbi,DC=local" } # Modify with the correct path
- "Executives" { $OUPath = "OU=Executives,DC=a18zagbi,DC=local" } # Modify with the correct path
- "HR" { $OUPath = "OU=HR,DC=a18zagbi,DC=local" } # Modify with the correct path
- "SALES" { $OUPath = "OU=sales,DC=a18zagbi,DC=local" } # Modify with the correct path
- Default { $OUPath = "OU=myusers,DC=a18zagbi,DC=local" }
- }
- $ADUserProperty = @{
- SamAccountName = $Login
- Name = $Name
- EmailAddress = $email
- Description = $description
- UserPrincipalName = $login
- DisplayName = $Name
- Enabled = $True
- Server = $Domain
- AccountPassword = $defpassword
- $Path = $OUPath
- } # Hashtable for splatting
- # Create user
- Write-Verbose "Creating user $Login"
- try {
- New-ADuser @ADUserProperty -ErrorAction Stop
- }
- catch {
- write-error $_
- } # try catch
- } # foreach
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement