1
+ param (
2
+ [Parameter (Mandatory , ValueFromPipelineByPropertyname )]
3
+ [ValidateNotNullOrEmpty ()]
4
+ [string ]$FirstName ,
5
+
6
+ [Parameter (Mandatory , ValueFromPipelineByPropertyname )]
7
+ [ValidateNotNullOrEmpty ()]
8
+ [string ]$LastName ,
9
+
10
+ [Parameter (Mandatory , ValueFromPipelineByPropertyname )]
11
+ [ValidateNotNullOrEmpty ()]
12
+ [string ]$MiddleInitial ,
13
+
14
+ [Parameter (Mandatory , ValueFromPipelineByPropertyname )]
15
+ [ValidateNotNullOrEmpty ()]
16
+ [string ]$Department ,
17
+
18
+ [Parameter (Mandatory , ValueFromPipelineByPropertyname )]
19
+ [ValidateNotNullOrEmpty ()]
20
+ [string ]$Title ,
21
+
22
+ [Parameter (ValueFromPipelineByPropertyname )]
23
+ [ValidateNotNullOrEmpty ()]
24
+ [string ]$Location = ' OU=Corporate Users' ,
25
+
26
+ [Parameter ()]
27
+ [ValidateNotNullOrEmpty ()]
28
+ [string ]$DefaultGroup = ' XYZCompany' ,
29
+
30
+ [Parameter ()]
31
+ [ValidateNotNullOrEmpty ()]
32
+ [string ]$DefaultPassword = ' p@$$w0rd12345' ,
33
+
34
+ [Parameter ()]
35
+ [ValidateScript ({ Test-Path - Path $_ })]
36
+ [string ]$BaseHomeFolderPath = ' \\MEMBERSRV1\Users'
37
+ )
38
+
39
+ # # Find the distinguished name of the domain the current computer is a part of.
40
+ $DomainDn = (Get-AdDomain ).DistinguishedName
41
+ # # Define the 'standard' username (first initial and last name)
42
+ $Username = " $ ( $FirstName.SubString (0 , 1 )) $LastName "
43
+
44
+ # region Check if an existing user already has the first initial/last name username taken
45
+ Write-Verbose - Message " Checking if [$ ( $Username ) ] is available"
46
+ if (Get-ADUser - Filter " Name -eq '$Username '" )
47
+ {
48
+ Write-Warning - Message " The username [$ ( $Username ) ] is not available. Checking alternate..."
49
+ # # If so, check to see if the first initial/middle initial/last name is taken.
50
+ $Username = " $ ( $FirstName.SubString (0 , 1 )) $MiddleInitial$LastName "
51
+ if (Get-ADUser - Filter " Name -eq '$Username '" )
52
+ {
53
+ throw " No acceptable username schema could be created"
54
+ }
55
+ else
56
+ {
57
+ Write-Verbose - Message " The alternate username [$ ( $Username ) ] is available."
58
+ }
59
+ }
60
+ else
61
+ {
62
+ Write-Verbose - Message " The username [$ ( $Username ) ] is available"
63
+ }
64
+ # endregion
65
+
66
+ # region Ensure the OU the user's going into exists
67
+ $ouDN = " $Location ,$DomainDn "
68
+ if (-not (Get-ADOrganizationalUnit - Filter " DistinguishedName -eq '$ouDN '" ))
69
+ {
70
+ throw " The user OU [$ ( $ouDN ) ] does not exist. Can't add a user there"
71
+ }
72
+ # endregion
73
+
74
+ # region Ensure the group the user's going into exists
75
+ if (-not (Get-ADGroup - Filter " Name -eq '$DefaultGroup '" ))
76
+ {
77
+ throw " The group [$ ( $DefaultGroup ) ] does not exist. Can't add the user into this group."
78
+ }
79
+ if (-not (Get-ADGroup - Filter " Name -eq '$Department '" ))
80
+ {
81
+ throw " The group [$ ( $Department ) ] does not exist. Can't add the user to this group."
82
+ }
83
+ # endregion
84
+
85
+ # region Ensure the home folder to create doesn't already exist
86
+ $homeFolderPath = " $BaseHomeFolderPath \$UserName "
87
+ if (Test-Path - Path $homeFolderPath )
88
+ {
89
+ throw " The home folder path [$homeFolderPath ] already exists."
90
+ }
91
+ # endregion
92
+
93
+ # region Create the new user
94
+ $NewUserParams = @ {
95
+ ' UserPrincipalName' = $Username
96
+ ' Name' = $Username
97
+ ' GivenName' = $FirstName
98
+ ' Surname' = $LastName
99
+ ' Title' = $Title
100
+ ' Department' = $Department
101
+ ' SamAccountName' = $Username
102
+ ' AccountPassword' = (ConvertTo-SecureString $DefaultPassword - AsPlainText - Force)
103
+ ' Enabled' = $true
104
+ ' Initials' = $MiddleInitial
105
+ ' Path' = " $Location ,$DomainDn "
106
+ ' ChangePasswordAtLogon' = $true
107
+ }
108
+ Write-Verbose - Message " Creating the new user account [$ ( $Username ) ] in OU [$ ( $ouDN ) ]"
109
+ New-AdUser @NewUserParams
110
+ # endregion
111
+
112
+ # region Add user to groups
113
+ Write-Verbose - Message " Adding the user account [$ ( $Username ) ] to the group [$ ( $DefaultGroup ) ]"
114
+ Add-ADGroupMember - Members $Username - Identity $DefaultGroup
115
+ Write-Verbose - Message " Adding the user account [$ ( $Username ) ] to the group [$ ( $Department ) ]"
116
+ Add-ADGroupMember - Members $Username - Identity $Department
117
+ # endregion
118
+
119
+ # region Create the home folder
120
+ Write-Verbose - message " Creating the home folder [$homeFolderPath ]..."
121
+ $null = mkdir $homeFolderPath
122
+ # endregion
0 commit comments