# Import-CSV & Set-ADUser Syntax

**URL:** <https://forums.powershell.org/t/import-csv-set-aduser-syntax/4973>\
**Category:** PowerShell Help\
**Created:** [October 5, 2015, 3:26am UTC](https://forums.powershell.org/t/import-csv-set-aduser-syntax/4973 "2015-10-05T03:26:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![karson-van-meeteren1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/karson-van-meeteren1/32/658_2.png) [@karson-van-meeteren1](https://forums.powershell.org/u/karson-van-meeteren1)\
**Post date:** [October 5, 2015, 3:26am UTC](https://forums.powershell.org/t/import-csv-set-aduser-syntax/4973/1 "2015-10-05T03:26:27Z")

</div>

Hello,  
I’ve got a csv I’m reading from with the following headers:

LastName FirstName OldUserName NewUserName Department EmailAddress OldHomeDirectory NewHomeDirectory OldDN NewDN olddisplayname newdisplayname

I’m unsure how to construct the variables from that CSV. ☹ Some examples on the Internet show $($headervalue) as the way, or can I access it like $variable.headervalue?

```
function Set-ARSEADProperties
{
	$debugPreference = "Continue"
	Write-Debug "Gathering Users"
	$userADList = Import-Csv \\fs10\home\kvanmeeteren\test_Migration_10_6_2015.csv | Select-Object *

	$psobjectCollection = @()
	foreach ($individualUser in $userADList)
	{
		#Gather variables
		$samaccountName = $individualUser.oldusername
		Write-Debug "Processing $samaccountName"
		$distinguishedName = $individualUser.olddn
		$firstName = $individualUser.firstname
		$lastName = $individualUser.lastname
		$newDN = $individualUser.newdn
		$firstNameDotLastName = $individualUser.newusername
		
		#change samaccountname, displayname, and clear profile path attribute.
		#finally move to new OU
		if ($samAccountName -ne $firstNameDotLastName)
		{
				Write-Debug "Performing migration on $samaccountName"
				Rename-ADObject -Identity $distinguishedName -NewName "$newDN" -ErrorAction Stop -WhatIf
				Get-ADUser $samAccountName | Set-ADUser -SamAccountName $firstNameDotLastName -DisplayName "$newDN" -HomeDirectory $null -PassThru -ErrorAction Stop -WhatIf | Move-ADObject -TargetPath "ou=newusers,dc=contoso,dc=com" -ErrorAction Stop -WhatIf
				
			}
			catch
			{
				Write-Host "Error migrating user. Press Ctrl+C to stop script or Enter to continue" -ForegroundColor Red
				Pause
			}
			finally
			{
				$newpath = "\\fs12\e$\home\$firstNameDotLastName"
			}
			
		}
		Write-Debug "Waiting for domain controllers to sync and verifying user migration is complete..."
		do
		{
			$newuserADList = Get-ADUser -SearchBase "ou=newusers,dc=contoso,dc=com" -Filter { samaccountname -like $firstNameDotLastName } -Properties Name, Surname, DistinguishedName, GivenName, SamAccountName, Description, HomeDirectory, Department | sort Surname
			Write-Host "Still waiting..." -ForegroundColor Cyan
			$samaccountstring = "$firstNameDotLastName"
			Start-Sleep -Seconds 5 -ErrorAction 'Ignore'
		}
		until ($newuserADList.surname -like '*')
		foreach ($newindividualUser in $newuserADList)
		{
			Write-Debug "Gathering post migration details"
			$newuserInfo = [ordered]@{
				LastName = $newindividualUser.Surname
				FirstName = $newindividualUser.GivenName
				NewSamAccountName = $newindividualUser.samaccountname
				OldSamAccountName = $individualUser.oldusername
				NewDisplayName = $newindividualUser.name
				OldDisplayName = $individualUser.olddisplayname
				NewHomeDirectoryAttribute = $newindividualUser.HomeDirectory
				OldHomeDirectoryAttribute = $individualUser.HomeDirectory
				NewPhysicalHomeDrivePath = $newpath
				OldPhysicalHomeDrivePath = $oldpath
				NewDistinguishedName = $newindividualUser.DistinguishedName
				OldDistinguishedName = $individualUser.olddn
			}
		}#end new user foreach
		$psObject = New-Object -TypeName System.Management.Automation.PSObject -property $newuserInfo
		$PSObjectCollection += $PSObject
	}#end foreach
	
	Write-Debug "Creating Results Output"
	$psobjectCollection | ogv -Title "Migration Results"
	$csvMigrationStatus = New-Item -ItemType File c:\Users\kvanmeeteren\Desktop\MigrationResults_$((get-date).toString('MM-dd-yyyy HH.mm.sstt')).csv
	$PSObjectCollection | Export-Csv -Path $csvMigrationStatus -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![karson-van-meeteren1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/karson-van-meeteren1/32/658_2.png) [@karson-van-meeteren1](https://forums.powershell.org/u/karson-van-meeteren1)\
**Post date:** [October 5, 2015, 3:33am UTC](https://forums.powershell.org/t/import-csv-set-aduser-syntax/4973/2 "2015-10-05T03:33:00Z")

</div>

One particular issue is getting the results after setting them.

```
DEBUG: Waiting for domain controllers to sync and verifying user migration is complete...
Get-ADUser : The search filter cannot be recognized
At \\fs10\home\kvanmeeteren\Powershell\InProgress\Set-ARSEADProperties.ps1:143 char:21
+ $newuserADList = Get-ADUser -SearchBase "ou=newusers..
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.Get
   ADUser
```

---

<div class="post-metadata">

**Author:** ![jeff-hayes](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@jeff-hayes](https://forums.powershell.org/u/jeff-hayes)\
**Post date:** [October 5, 2015, 3:53am UTC](https://forums.powershell.org/t/import-csv-set-aduser-syntax/4973/3 "2015-10-05T03:53:42Z")

</div>

Update this line  
$newuserADList = Get-ADUser -SearchBase “ou=newusers,dc=contoso,dc=com” -Filter { samaccountname -like $firstNameDotLastName } -Properties Name, Surname, DistinguishedName, GivenName, SamAccountName, Description, HomeDirectory, Department | sort Surname

to

$newuserADList = Get-ADUser -SearchBase “ou=newusers,dc=contoso,dc=com” -Filter { samaccountname -like “$firstNameDotLastName” } -Properties Name, Surname, DistinguishedName, GivenName, SamAccountName, Description, HomeDirectory, Department | sort Surname

Or you might need to make the sam account name in the excel spread sheet in " "

I just tired this

$sam = “Jeffery.Test”

PS H:&gt; Get-ADUser -Filter {Samaccountname -eq “$sam”}

PS H:&gt; Get-ADUser -Filter {Samaccountname -eq $sam}

DistinguishedName : CN=Jeffery, Test,OU=TEST,OU=CTM,OU=CSD,OU=COACD,DC=coacd,DC=org  
Enabled : True  
GivenName : Jeffery  
Name : Jeffery, Test  
ObjectClass : user  
ObjectGUID : 8d3a5c82-69f3-4be2-b7e6-d86df7c96326  
SamAccountName : Jeffery.Test  
SID : S-1-5-21-682003330-789336058-2146660071-97636  
Surname : Test  
UserPrincipalName : [Jeffery.Test@coacd.org](mailto:Jeffery.Test@coacd.org)

PS H:&gt; $list = Get-ADUser -Filter {Samaccountname -eq $sam}

PS H:&gt; $list

DistinguishedName : CN=Jeffery, Test,OU=TEST,OU=CTM,OU=CSD,OU=COACD,DC=coacd,DC=org  
Enabled : True  
GivenName : Jeffery  
Name : Jeffery, Test  
ObjectClass : user  
ObjectGUID : 8d3a5c82-69f3-4be2-b7e6-d86df7c96326  
SamAccountName : Jeffery.Test  
SID : S-1-5-21-682003330-789336058-2146660071-97636  
Surname : Test  
UserPrincipalName : [Jeffery.Test@coacd.org](mailto:Jeffery.Test@coacd.org)

---

<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:44pm UTC](https://forums.powershell.org/t/import-csv-set-aduser-syntax/4973/4 "2024-05-16T20:44:44Z")

</div>

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


