-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdd remove users.txt
63 lines (51 loc) · 2.26 KB
/
Add remove users.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#David Lee, Lorezen, 10/29/21, assigment 8
#this script is modified from this walkthrough
#https://www.alitajran.com/create-active-directory-users-from-csv-with-powershell/#h-check-the-csv-file
# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
# Store the data from csv in the $ADUsers variable
$ADUsers = Import-Csv C:\Users\winSever19\Desktop\Assigment8userList.csv -Delimiter ","
# Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
#Read user data from each field in each row and assign the data to a variable as below
$username = $User.username
$password = $User.password
$firstname = $User.firstname
$lastname = $User.lastname
$OU = "OU=users, OU=Accounts, DC=DLee, DC=local" #This field refers to the OU the user account is to be created in
$add = "Add"
$remove= "Remove"
if ($add -eq $user.action)
{
# check action if add run this
if (Get-ADUser -F { SamAccountName -eq $username }) {
# If user does exist, give a warning
Write-Warning "A user account with username $username already exists in Active Directory."
Read-Host -Prompt "checked if user exists"
}
else {
# User does not exist then proceed to create the new user account
# Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $username `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-DisplayName "$lastname, $firstname" `
-Path $OU `
-Description $Description `
# If user is created, show message.
Write-Host "The user account $username is created." -ForegroundColor Cyan
Read-Host -Prompt "tried to add $username"
}
}
#if action==remove
if ($Remove -eq $user.action)
{
Remove-ADUser -Identity $username `
# If user is created, show message.
Write-Host "The user account $username is removed." -ForegroundColor Cyan
Read-Host -Prompt "tried to remove $username "
}
}
Read-Host -Prompt "Press Enter to exit"