I’ve been using this powershell script to import users in Acrive Directory and have always had this error pop up when I first run it saying:
“Import-Module : A positional parameter cannot be found that accepts argument ‘Directory’.
At C:ADMGMT2025 AD Import File.ps1:2 char:1
- Import-Module Active Directory
-
+ CategoryInfo : InvalidArgument: (:) [Import-Module], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand"
I can’t disgnose the problem.
**Here is the code:
**
# Import required modules
Import-Module Active Directory
# Prompt user for CSV file path
$filepath = Read-Host -Prompt "Choose path for CSV File"
# Import the file into a variable
$users = Import-CSV $filepath
# Loop through each row and gather information
ForEach ($user in $users) {
# Gather the user's information
$fname = $user.'First Name'
$lname = $user.'Last Name'
$emailaddress = $user.'Email Address'
$Password = $user.Password
$UserPrincipalName = $user.'User Logon Name'
$Description = $user.'Description'
$OUpath = $user.'Organizational Unit'
$UserPrincipalName = $user.'User Logon Name'
#Create new AD user for each new user in CSV file
New-ADUser -Name "$fname $lname" -GivenName $fname -Surname $lname -UserPrincipalName $UserPrincipalName -Path $OUpath -AccountPassword (convertto-securestring $password -AsPlainText -Force) -ChangePasswordAtLogon $False -Description $Description -CannotChangePassword $True -EmailAddress $emailaddress -DisplayName "$fname $Lname" -PasswordNeverExpires $True -SamAccountName $UserPrincipalName -Enabled $True
}
ForEach ($user in $users)
{
#Add the user to the correct student Groups one by one
Add-ADGroupMember -Identity $User.Group -Members $User.'User Logon Name'
Add-ADGroupMember -Identity $User.Group2 -Members $User.'User Logon Name'
#Echo output for each new user
echo "Account created for $fname $lname in $OUpath"
}```
I've tried going through the whole script but couldn't find any errors. I've also tried looking at other similar issues which have been posted here but couldn't find any relevant info to troubleshoot with.
New contributor
Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.