Hello Experts,

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.

If I then put the relevant file on the server and change the location, the pw script works. Am I missing something?

This is my script":

Import active directory module for running AD cmdlets

Import-Module ActiveDirectory

Store the data from NewUsersFinal.csv in the $ADUsers variable

$ADUsers = Import-Csv -Path \TECHCOP.nl\Shares\HR\onboarding-employes\ad.csv -Delimiter “;”

Define TECHCOP

$UPN = “TECHCOP”

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
$initials = $User.initials
$OU = $User.ou #This field refers to the OU the user account is to be created in
$email = $User.email

Check to see if the user already exists in AD

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.”
}
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
-UserPrincipalName “$username@$TECHCOP” -Name "$firstname $lastname"
-GivenName $firstname -Surname $lastname
-Initials $initials -Enabled $True
-DisplayName “$lastname, $firstname” -Path $OU
-EmailAddress $email `
-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True

If user is created, show message.

Write-Host “The user account $username is created.” -ForegroundColor Cyan
}
}

Read-Host -Prompt “Press Enter to exit”

7 Spice ups

If you post code, please use the ‘Insert Code’ button. Please and thank you!

192033ab-bb8f-4032-88a5-8e2313af0344-codebutton_small.png

  • Neally Bot

What error do you get?

1 Spice up

The Path parameter expects a String.

You didn’t give it one.

1 Spice up

also backticks are bad, use splatting, more like so

# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
 
# Store the data from NewUsersFinal.csv in the $ADUsers variable
$ADUsers = Import-Csv "\\TECHCOP.nl\Shares\HR\onboarding-employes\ad.csv" -Delimiter ";"

# Define TECHCOP
$UPN = "TECHCOP.com"

# Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
  # Check to see if the user already exists in AD
    if (Get-ADUser -Filter "SamAccountName -eq '$($User.username)'") {   
        # If user does exist, give a warning
        Write-Warning "A user account with username '$($User.username)' already exists in Active Directory."
    }
    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
        $newUser = @{
            SamAccountName        = "$($User.username)"
            UserPrincipalName     = "$($User.username)@$upn" 
            Name                  = "$($User.firstname) $($User.lastname)" 
            GivenName             = "$($User.firstname)"
            Surname               = "$($User.lastname)"
            Initials              = "$($User.initials)"
            Enabled               = $True 
            DisplayName           = "$($User.lastname), $($User.firstname)" 
            Path                  = "$($user.ou)"
            EmailAddress          = "$($User.email)"
            AccountPassword       = (ConvertTo-secureString "$($User.password)" -AsPlainText -Force) 
            ChangePasswordAtLogon = $True
        }
    
        try {
            New-ADUser @newUser -ErrorAction Stop
            Write-Output "The user account '$username' is created."
        }
        catch {
            $error[0].exception.message
        }
    }
}

Read-Host -Prompt "Press Enter to exit"

@alexw ​, thanks!

I also use the AGDLP principle in my server environment, is it wise to add (users) to a GlobalGroup using powershell in the same script?

put your path in quotes

$ADUsers = Import-Csv -Path "\\TECHCOP.nl\Shares\HR\onboarding-employes\ad.csv" -Delimiter ";"
1 Spice up

You can add people to groups with ‘add-adgroupmember’ if they need to get added or not highly depends on your environment.
We have certain groups people are automatically added to for all company communication and such.

@alexw ​, thanks! I’m currently working on automatically adding a homeprofile folder, but I run into this every time.

I would like every new user to automatically get its own Profile folder, with Driveletter P:

only executing the script below fails every time

$ADUsers = Import-Csv "\\TECHCOP.nl\Shares\HR\onboarding-employes\ad.csv" -Delimiter ";"

foreach ($User in $UserList) {

    $Account = Get-ADUser -LDAPFilter ('(&(displayname={0}))' -f $User.DisplayName);

    $HomeDirectory = '\\TECHCOP.nl\Home\%username%{0}' -f $Account.SamAccountName;

    Set-ADUser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive P;
}

how does it fail? do you get an error?

$ADUsers = Import-Csv "\\TECHCOP.nl\Shares\HR\onboarding-employes\ad.csv" -Delimiter ";"

foreach ($User in $ADUsers ) {
    $account = $null
    $account = Get-ADUser -Filter "displayname -eq '$($User.DisplayName)'"

    if($account){
        try{
            $setHome =@{
                Identity      = $Account.SamAccountName
                HomeDirectory = $HomeDirectory 
                HomeDrive     = "P:"
            }
            Set-ADUser @setHome -ErrorAction Stop
        }
        catch{
            $Error[0].exception.message
        }
    }
}

Ummm…

If you’re reading in your list of Users thus:

… how does the $ADUsers variable get into $UserList?

That would lead to a lot of errors…

1 Spice up