Hello Experts,<\/p>\n
I recently started practicing powershell. In this case I started importing users from a CSV file. Only I keep getting an error message when importing the CSV file in question. This is because I put the location of the file on a DFS share.<\/p>\n
If I then put the relevant file on the server and change the location, the pw script works. Am I missing something?<\/p>\n
This is my script\":<\/p>\n
Import-Module ActiveDirectory<\/p>\n
$ADUsers = Import-Csv -Path \\TECHCOP.nl\\Shares\\HR\\onboarding-employes\\ad.csv -Delimiter “;”<\/p>\n
$UPN = “TECHCOP”<\/p>\n
foreach ($User in $ADUsers) {<\/p>\n
#Read<\/span> user data from each field in each row and assign the data to a variable as below if (Get-ADUser -F { SamAccountName -eq $username }) {<\/p>\n Write-Warning “A user account with username $username already exists in Active Directory.” New-ADUser Write-Host “The user account $username is created.” -ForegroundColor Cyan Read-Host -Prompt “Press Enter to exit”<\/p>","upvoteCount":7,"answerCount":11,"datePublished":"2022-12-29T19:38:26.000Z","author":{"@type":"Person","name":"spiceuser-4qei9","url":"https://community.spiceworks.com/u/spiceuser-4qei9"},"suggestedAnswer":[{"@type":"Answer","text":" Hello Experts,<\/p>\n I recently started practicing powershell. In this case I started importing users from a CSV file. Only I keep getting an error message when importing the CSV file in question. This is because I put the location of the file on a DFS share.<\/p>\n If I then put the relevant file on the server and change the location, the pw script works. Am I missing something?<\/p>\n This is my script\":<\/p>\n Import-Module ActiveDirectory<\/p>\n $ADUsers = Import-Csv -Path \\TECHCOP.nl\\Shares\\HR\\onboarding-employes\\ad.csv -Delimiter “;”<\/p>\n $UPN = “TECHCOP”<\/p>\n foreach ($User in $ADUsers) {<\/p>\n #Read<\/span> user data from each field in each row and assign the data to a variable as below if (Get-ADUser -F { SamAccountName -eq $username }) {<\/p>\n Write-Warning “A user account with username $username already exists in Active Directory.” New-ADUser Write-Host “The user account $username is created.” -ForegroundColor Cyan Read-Host -Prompt “Press Enter to exit”<\/p>","upvoteCount":7,"datePublished":"2022-12-29T19:38:26.000Z","url":"https://community.spiceworks.com/t/powershell-script-ad-users/943143/1","author":{"@type":"Person","name":"spiceuser-4qei9","url":"https://community.spiceworks.com/u/spiceuser-4qei9"}},{"@type":"Answer","text":" If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n
\n$username = $User.username
\n$password = $User.password
\n$firstname = $User.firstname
\n$lastname = $User.lastname
\n$initials = $User.initials
\n$OU = $User.ou #This<\/span> field refers to the OU the user account is to be created in
\n$email = $User.email<\/p>\n<\/a>Check to see if the user already exists in AD<\/h1>\n
<\/a>If user does exist, give a warning<\/h1>\n
\n}
\nelse {<\/p>\n<\/a>User does not exist then proceed to create the new user account<\/h1>\n
<\/a>Account will be created in the OU provided by the $OU variable read from the CSV file<\/h1>\n
-SamAccountName $username<\/code>
\n-UserPrincipalName “$username@$TECHCOP” -Name \"$firstname $lastname\"<\/code>
\n-GivenName $firstname -Surname $lastname<\/code>
\n-Initials $initials -Enabled $True<\/code>
\n-DisplayName “$lastname, $firstname” -Path $OU<\/code>
\n-EmailAddress $email `
\n-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True<\/p>\n<\/a>If user is created, show message.<\/h1>\n
\n}
\n}<\/p>\n<\/a>Import active directory module for running AD cmdlets<\/h1>\n
<\/a>Store the data from NewUsersFinal.csv in the $ADUsers variable<\/h1>\n
<\/a>Define TECHCOP<\/h1>\n
<\/a>Loop through each row containing user details in the CSV file<\/h1>\n
\n$username = $User.username
\n$password = $User.password
\n$firstname = $User.firstname
\n$lastname = $User.lastname
\n$initials = $User.initials
\n$OU = $User.ou #This<\/span> field refers to the OU the user account is to be created in
\n$email = $User.email<\/p>\n<\/a>Check to see if the user already exists in AD<\/h1>\n
<\/a>If user does exist, give a warning<\/h1>\n
\n}
\nelse {<\/p>\n<\/a>User does not exist then proceed to create the new user account<\/h1>\n
<\/a>Account will be created in the OU provided by the $OU variable read from the CSV file<\/h1>\n
-SamAccountName $username<\/code>
\n-UserPrincipalName “$username@$TECHCOP” -Name \"$firstname $lastname\"<\/code>
\n-GivenName $firstname -Surname $lastname<\/code>
\n-Initials $initials -Enabled $True<\/code>
\n-DisplayName “$lastname, $firstname” -Path $OU<\/code>
\n-EmailAddress $email `
\n-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True<\/p>\n<\/a>If user is created, show message.<\/h1>\n
\n}
\n}<\/p>\n