Hello guys, its me again. Thanks in advance for all your help. I am working on a project where I am updating all user Titles in AD. I have a imported file from which I am getting the corrected Titles.
Attached you will find a copy of my code as well as the error that is being received. When executing the code I receive a error indicating that $._name is not defined. I feel like I’m close. What am I doing wrong?
Write-Host "processing"
#Imports information from CSV file
$Users = Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv"
#grabs information from imported Csv file
Get-Content "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv" |
#For each user updated the old description with the new Description from CSV file
foreach-object {$_ -replace "Description", "new.Description"}
Get-AdUser -filter{name -eq $_.name}|Set-ADUser -Description $_.Description |
#Set-Content "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv"|
#Exports results back to desired location
Export-Csv "C:\WSOL_Open_Migration\Updated_AD.csv" -whatif
1 Spice up
Neally
(Neally)
2
It’s important to know what your CSV looks like, does it have headers?
You do both import content and import-csv, that’s redundant.
I don’t see the error message you mentioned?
Neally
(Neally)
3
This assumes the CSV to look like this:
samaccountname,description
jdoe,supercool description
foreach($ADUser in (Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv")){
Set-aduser -Identity $aduser.samaccountname -Description $ADUser.description -Verbose
}
Hey Neally, I tried your suggestion. I am receiving a error ( see screenshot below). I am flabber-gasted. I have tried this code 6 ways to Sunday and everything continues to error out. yes there are headers on my csv file. (Country, Name, Preferred Name, Description, Supervisor, Primary Address). I didnt realize Powershell had such a steep learning curve. What am I doing wrong?
Write-Host "processing"
#Imports information from CSV file
$AdUser= @()
$AdUser = Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv"
Foreach ($AdUser in (Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv" ))
{Set-aduser -Identity $Aduser.name -Description $AdUser.Description}
#Exports results back to desired location
Export-Csv "C:\WSOL_Open_Migration\TestResults.csv" -NoTypeInformation
Neally
(Neally)
5
You CSV has the full name, not a samaccount
“Morgan, Alicia” is the name, “amorgan” could be a samaccountname.
I’d highly recommend do rather go of the samaccountname, than the actual name.
if(get-aduser -filter "name -like '$($aduser.samaccountname)'"){
set-aduser #yadi yada
}
else{
write-output "User '$($aduser.samaccountname)' not found."
}
Hey Neally thanks again. I have followed your suggestions and this is the error code I’m receiving now.
Write-Host "processing"
#Imports information from CSV file
$AdUser= @()
$AdUser = Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv"
Foreach ($AdUser in (Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv" ))
{Set-aduser -Identity $Aduser.name -Description $AdUser.Description
if (Get-AdUser -filter "name -like '$($AdUser.samaccountname)'"){
Set-aduser -Identity $Aduser.name -Description $AdUser.Description -WhatIf
}
else{
Write-Output "User '$($AdUser.samaccountname) 'not found."
}}
#Exports results back to desired location
Export-Csv "C:\WSOL_Open_Migration\TestResults.csv" -NoTypeInformation
@powershellman8045 @jitensh
Neally
(Neally)
7
-like is a wildcard, so you want to add *
Foreach ($AdUser in (Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv" )){
if (Get-AdUser -filter "name -like '*$($AdUser.samaccountname)*'"){
Set-aduser -Identity $Aduser.name -Description $AdUser.Description -WhatIf
}
else{
Write-Output "User '$($AdUser.samaccountname) 'not found."
}
}
Neally
(Neally)
8
What does your CSV look like? Can you post the first 2 rows (sanitized), that might help.
Neally
(Neally)
10
Ugh… it has no username in your CSV…
try like so
Foreach ($AdUser in (Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv" )){
if (Get-AdUser -filter "name -like '*$($AdUser.name)*'"){
Set-aduser -Identity $Aduser.name -Description $AdUser.Description -Verbose
}
else{
Write-Output "User '$($AdUser.name) 'not found."
}
}
jitensh
(JitenSh)
11
you should have samaccountname in CSV can you do this you will
get a new csv just put the description here against all copying from the csv you have, it should be easy if it comes in order select the entire column
$list = (import-csv C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD.csv|select -exp name) -replace ',',' '
ForEach($user in $list){
Try{
Get-ADUser -Filter { displayName -like $user } -ErrorAction stop | Select samaccountname,name |export-csv C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD2.csv -nti
}
Catch
{$_ |Out-File c:\notfound.txt -Append}
JitenSh, can you private message me please?
@jitensh
jitensh
(JitenSh)
13
now you wil get a new csv name Active_Employees_AD2.csv with samaccountname,name after adding description
you can set it very easily using
edited: copied neally’s to save time
foreach($ADUser in (Import-Csv "C:\Users\AliciaM\Documents\APFM_Projects\Active_Employees_AD2.csv")){
Set-aduser -Identity $aduser.samaccountname -Description $ADUser.description -Verbose
}
psophos
(M Boyle)
14
In your csv file, are the ntries under name in this format?:
surname, firstname
If so, is that the format that the data is stored in the name property in AD?
If not, the searching will fail until the input data is re-ordered.