# Need help , updating data in AD from a Csv file

**URL:** <https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745>\
**Category:** PowerShell Help\
**Created:** [October 14, 2021, 4:21pm UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745 "2021-10-14T16:21:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nen4ik](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nen4ik](https://forums.powershell.org/u/nen4ik)\
**Post date:** [October 14, 2021, 4:21pm UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/1 "2021-10-14T16:21:35Z")

</div>

Hi, Need help , updating data in AD from a Csv file  
Thank you.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 14, 2021, 5:50pm UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/2 "2021-10-14T17:50:30Z")

</div>

Hi Denis, welcome to the forum 👋

What sort of help do you need?

Please post the script you’re having problems with, together with any errors, and we’ll be happy to take a look at it for you and provide assistance.

When posting the script and errors, please make sure to use the \</\> button and paste the information where shown. This makes it easier for us to read, and to copy and paste your code. TIA.

---

<div class="post-metadata">

**Author:** ![nen4ik](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nen4ik](https://forums.powershell.org/u/nen4ik)\
**Post date:** [October 15, 2021, 5:20am UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/3 "2021-10-15T05:20:03Z")

</div>

Import-Module ActiveDirectory  
$Users = Import-Csv -Path C:\Users\denbut\Desktop\darb.csv  
$users = Get-ADUser -Filter \* -SearchBase “LDAP://OU=\_users/staff,DC=ism,DC=lt”  
Get-Content -Path C:\Users\denbut\Desktop\test.csv | ft  
foreach  
($dataRecord in $datasource){  
$cn=$dataRecord.FirstName + " " + $dataRecord.LastName  
$sAMAccountname=$dataRecord.FirstName + “.” + $dataRecord.LastName  
$givenName=$dataRecord.FirstName

$sn=$dataRecord.LastName

$sAMAccountName=$sAMAccountName.ToLower()

$displayName=$sn +", " + $givenName  
$userPrincipalName=$sAMAccountName + “@ism.lt”  
$physicalDeliveryOfficeName=$dataRecord.office

$objuser.Put(“sAMAccountName,$sAMAccountName”)  
$objuser.Put("userPrincipalName”,$userPrincipalName)  
$objuser.Put(“displayname”, $displayName)  
$objuser.Put(“givenNane”, $givenName)  
$objuser.Put("sn”,$sn)  
$objuser.Put("physicalDeliveryOfficeName”,$physicalDeliveryOfficeName)  
$objuser.Put(“telephoneNumber”,$telephoneNumber)  
$objuser.SetInfo()  
}  
problem  
Get-ADUser : Directory object not found  
At C:\Users\denis\Documents\Untitled11.ps1:3 char:10

- $users = Get-ADUser -Filter \* -SearchBase "LDAP://OU=\_users/staff,DC= …
- 

```
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : ObjectNotFound: (🙂 [Get-ADUser], ADIdentityNotFoundException
  - FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 15, 2021, 7:52am UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/4 "2021-10-15T07:52:37Z")

</div>

First, as previously requested: please use the \</\> button to format the code in your post. You can edit your post, you do not need to create a new one to reformat it.

Your script not only contains errors but appears to be incomplete.

`$Users` is being set to the contents of the darb.csv, then over written with the results from `Get-ADUser` on the next line.

`$datasource` is not defined in the script you’ve posted.

`$objuser` is not defined in the script you’ve posted. But there’s no need to do it that way anyway when you have the `Set-ADUser` cmdlet.

The error you’re getting for the `Get-ADUser` command is due to the way you’ve defined the `-SearchBase` parameter. You should not include the `LDAP://` part in the string, it’s just the DN of the OU.

> **[Get-ADUser (ActiveDirectory)](https://docs.microsoft.com/en-us/powershell/module/activedirectory/get-aduser?view=windowsserver2019-ps)**
>
> Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.

> **[Set-ADUser (ActiveDirectory)](https://docs.microsoft.com/en-us/powershell/module/activedirectory/set-aduser?view=windowsserver2019-ps)**
>
> Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.

---

<div class="post-metadata">

**Author:** ![nen4ik](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nen4ik](https://forums.powershell.org/u/nen4ik)\
**Post date:** [October 15, 2021, 8:30am UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/5 "2021-10-15T08:30:46Z")

</div>

Can these line be successful?

```auto
$objuser. Put("sAMAccountName,$sAMAccountName")
$objuser. Put("userPrincipalName", $userPrincipalName)
$objuser. Put("displayname", $displayName)
$objuser. Įdėti ("givenNane", $givenName)
$objuser. Put("sn", $sn)
$objuser. Put("physicalDeliveryOfficeName", $physicalDeliveryOfficeName)
$objuser. Įdėti ("phoneNumber", $telephoneNumber)
$objuser. SetInfo()

```

And I did’t really understand about the variable $datasource ?  
Now the code looks like this …

```auto
$Users = Import-Csv -Path C:\Users\denbut\Desktop\darb.csv
Get-Content -Path C:\Users\denbut\Desktop\darb.csv 
$objIO=[ADSI]"LDAP://OU=ism.lt/_users/staff/staff,DC=ism,DC=lt"
Get-ADUser -Filter * -SearchBase "ou=_users/staff,dc=ism,dc=lt"

foreach($dataRecord in $datasource){
$cn=$dataRecord.FirstName + " " + $dataRecord.LastName
$sAMAccountname=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$sn +", " + $givenName
$userPrincipalName=$sAMAccountName + “@ism.lt”
$objuser=$objOU.Create("user","CN="+$cn)
$physicalDeliveryOfficeName=$dataRecord.office

}

#$objuser.Put("sAMAccountName,$sAMAccountName")
#$objuser.Put("userPrincipalName”,$userPrincipalName)
#$objuser.Put(“displayname”, $displayName)
#$objuser.Put(“givenNane”, $givenName)
#$objuser.Put("sn”,$sn)
#$objuser.Put("physicalDeliveryOfficeName”,$physicalDeliveryOfficeName)
#$objuser.Put("telephoneNumber",$telephoneNumber)
#$objuser.SetInfo()

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 15, 2021, 9:21am UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/6 "2021-10-15T09:21:17Z")

</div>

No, that won’t do what you want either.

You don’t need to use the `Put()` and `SetInfo()` methods.  
Have a look at the help file for `Set-ADUser` it gives examples of how to use it.

> [@nen4ik](#):
>
> And I did’t really understand about the variable $datasource ?

Well you have a loop, that says `foreach($dataRecord in $datasource)` but there are no records in `$datasource` because you don’t assign it any value in your code. I would expect to see something like this in your script:

```auto
$datasource = Import-Csv C:\Temp\userdata.csv

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/need-help-updating-data-in-ad-from-a-csv-file/17745/7 "2024-05-16T20:21:11Z")

</div>

Closed on May 16, 2024, 8:21pm UTC


