Hey guys, i have been struggling with a script for a while now and i cant figure it out for the life of me. I am new to Powershell or any sort of scripting for that matter.

I have 3 questions regarding this code.

  1. How do i change the $User.username to be the first letter of “Voornaam” and 2 letters of “Achternaam”
  2. How do set the OU to be read from “Werkgever”. Simply put ; if variable is A output OU=1…
    Else if Variable is B output OU=2…
  3. How do i link a manager to a user? Found in the Organization tab - Manager - Name.

Thank you all for those who take the time to help a beginner like me, if there are any questions please let me know, i’m in desperate need of help! I’m sorry for the bad english since it’s not my native tongue

Here is my .csv File.

Here is my code ;

Import-Module activedirectory
  
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:/Eigen_poging_tot_bulkimport.csv

#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

	$Voornaam 	      = $User.firstname
	$Achternaam       = $User.lastname	
    $Voornaam,$Achternaam = $User.username
    $email            = $User.email
    $streetaddress    = $User.streetaddress
    $city             = $User.city
    $zipcode          = $User.zipcode
    $state            = $User.state
    $country          = $User.country
    $Mobiel_werk      = $User.telephone
    $Functie          = $User.jobtitle
    $Werkgever        = $User.company
    $Locatie_werkzaam = $User.department
    $Password         = $User.Password

 if ($Werkgever -eq "GP_Groot_bv")
     {-Path = "OU=01. GP GROOT BV,OU=GP GROOT ORGANISATIE,DC=testomgeving,DC=nl"}      
   ElseIf ($Werkgever  -eq 'GP Groot brandstoffen en oliehandel bv')  
           { $User.OU = 'OU=04. BRANDSTOFFEN EN OLIEHANDEL,OU=GP GROOT ORGANISATIE,DC=testomgeving,DC=nl' }
    ElseIf ($Werkgever  -eq 'NNRD')  
           { -Path = 'OU=07. NNRD,OU=GP GROOT ORGANISATIE,DC=testomgeving,DC=nl' }
        
             Else {

  'The OU Is not defined properly'

}

	#Check to see if the user already exists in AD
	if (Get-ADUser -F {SamAccountName -eq "$Firstname $Lastname"})
	{
		 #If user does exist, give a warning
		 Write-Warning "A user account with username $Username already exist in Active Directory."
	}
	else
	{
		#User does not exist then proceed to create the new user account
		New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Username@testomgeving.nl" `
            -Name "$Firstname $Lastname" `
            -GivenName $Firstname `
            -Surname $Lastname `
            -Enabled $True `
            -DisplayName "$Lastname, $Firstname" `
            -Path `
            -City $city `
            -Company $company `
            -State $state `
            -StreetAddress $streetaddress `
            -OfficePhone $telephone `
            -EmailAddress $email `
            -Title $jobtitle `
            -Department $department `
            -AccountPassword (convertto-securestring "TempPass123!" -AsPlainText -Force) -ChangePasswordAtLogon $True `
     }       
	}


5 Spice ups

Something like:

Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable$ADUsers = Import-csv C:/Eigen_poging_tot_bulkimport.csv
#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers){
    if ($Werkgever -eq "GP_Groot_bv") {
        $Path = "OU=01. GP GROOT BV,OU=GP GROOT ORGANISATIE,DC=testomgeving,DC=nl"
    } ElseIf ($Werkgever  -eq 'GP Groot brandstoffen en oliehandel bv')  {
        $Path =  'OU=04. BRANDSTOFFEN EN OLIEHANDEL,OU=GP GROOT ORGANISATIE,DC=testomgeving,DC=nl'}
    ElseIf ($Werkgever  -eq 'NNRD') {
        $Path = 'OU=07. NNRD,OU=GP GROOT ORGANISATIE,DC=testomgeving,DC=nl'
    }
    ...
    ...
    -Path $PATH `

You’d use a substrings and subexpression operator

$user.username = "$(($user.voorname).substring(0,1))$(($user.Achternaam).substring(0,2))"

$user.username

See Gary’s suggestion

Not entirely sure on your question, but this is how you set the manager

Set-ADUser $Username -Manager $Manager