Import-module ActiveDirectory\n$users = import-csv \"C:\\ad_import\\users.csv\"\nforeach ($user in $users) {\nset-aduser -identity $user.samaccountname -givenName $user.FirstName -surname $user.LastName -title $user.JobTitle -description $user.description -state $user.State -Office $user.Office -department $user.Department -city $user.City -manager $user.manager -company $user.company\n\n}\n<\/code><\/pre>\n
It happens that some existing values in AD needs to be cleared for some users, so if I left a cell blank in the csv file I receive an error related to -replace parameter. I tried a lot but couldn’t fix it.<\/p>","upvoteCount":3,"answerCount":6,"datePublished":"2017-09-06T18:53:59.000Z","author":{"@type":"Person","name":"3abmula","url":"https://community.spiceworks.com/u/3abmula"},"acceptedAnswer":{"@type":"Answer","text":"
Not tested<\/p>\n
Import-module ActiveDirectory\n$users = Import-Csv \"C:\\ad_import\\users.csv\"\nforeach ($user in $users) {\n $props = @{\n identity = $user.samaccountname\n givenName = if($user.FirstName ){$user.FirstName }else{$null}\n surname = if($user.LastName ){$user.LastName }else{$null}\n title = if($user.JobTitle ){$user.JobTitle }else{$null}\n description = if($user.description){$user.description}else{$null}\n state = if($user.State ){$user.State }else{$null}\n Office = if($user.Office ){$user.Office }else{$null}\n department = if($user.Department ){$user.Department }else{$null}\n city = if($user.City ){$user.City }else{$null}\n manager = if($user.manager ){$user.manager }else{$null}\n company = if($user.company ){$user.company }else{$null}\n }\n set-aduser @props\n}\n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2017-09-06T19:08:44.000Z","url":"https://community.spiceworks.com/t/help-updating-ad-attributes-from-csv-file/604314/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"I have a simple script<\/p>\n
Import-module ActiveDirectory\n$users = import-csv \"C:\\ad_import\\users.csv\"\nforeach ($user in $users) {\nset-aduser -identity $user.samaccountname -givenName $user.FirstName -surname $user.LastName -title $user.JobTitle -description $user.description -state $user.State -Office $user.Office -department $user.Department -city $user.City -manager $user.manager -company $user.company\n\n}\n<\/code><\/pre>\nIt happens that some existing values in AD needs to be cleared for some users, so if I left a cell blank in the csv file I receive an error related to -replace parameter. I tried a lot but couldn’t fix it.<\/p>","upvoteCount":3,"datePublished":"2017-09-06T18:53:59.000Z","url":"https://community.spiceworks.com/t/help-updating-ad-attributes-from-csv-file/604314/1","author":{"@type":"Person","name":"3abmula","url":"https://community.spiceworks.com/u/3abmula"}},{"@type":"Answer","text":"
Hi JitenSh,<\/p>\n
Maybe I wasn’t able to explain my requirements properly but I actually have some columns in the csv file has values for some users and empty for others, the empty values needs to clear an existing AD values if it does have value. so for example if I set -state $null that will clear State AD field for all users.<\/p>","upvoteCount":0,"datePublished":"2017-09-06T19:13:28.000Z","url":"https://community.spiceworks.com/t/help-updating-ad-attributes-from-csv-file/604314/3","author":{"@type":"Person","name":"3abmula","url":"https://community.spiceworks.com/u/3abmula"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Muhammad Abd Elmawla:<\/div>\n
\nHi JitenSh,<\/p>\n
Maybe I wasn’t able to explain my requirements properly but I actually have some columns in the csv file has values for some users and empty for others, the empty values needs to clear an existing AD values if it does have value. so for example if I set -state $null that will clear State AD field for all users.<\/p>\n<\/blockquote>\n<\/aside>\n
empty value doesn’t mean anything,I didn’t say about all users only those users whom you want to clear some fields.<\/p>\n
samaccountname,title,department \njohn, teamlead,IT \nNedd,accountnant,$null<\/p>","upvoteCount":0,"datePublished":"2017-09-06T19:20:27.000Z","url":"https://community.spiceworks.com/t/help-updating-ad-attributes-from-csv-file/604314/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"\n\n
<\/div>\n
JitenSh:<\/div>\n
\n\n\n
<\/div>\n
Muhammad Abd Elmawla:<\/div>\n
\nHi JitenSh,<\/p>\n
Maybe I wasn’t able to explain my requirements properly but I actually have some columns in the csv file has values for some users and empty for others, the empty values needs to clear an existing AD values if it does have value. so for example if I set -state $null that will clear State AD field for all users.<\/p>\n<\/blockquote>\n<\/aside>\n
empty value doesn’t mean anything, assume that you have csv I didn’t say about all users only those users whom you want to clear some fields.<\/p>\n
samaccountname,title,department \njohn, teamlead,IT \nNedd,accountnant,$null<\/p>\n<\/blockquote>\n<\/aside>\n
Unfortunately that wouldn’t help in my case because it’s an automated process and the csv file i receive from HR system in a specified share folder, and I have a scheduled task for the script runs daily. Thanks for your help anyway <\/p>","upvoteCount":0,"datePublished":"2017-09-06T19:26:16.000Z","url":"https://community.spiceworks.com/t/help-updating-ad-attributes-from-csv-file/604314/5","author":{"@type":"Person","name":"3abmula","url":"https://community.spiceworks.com/u/3abmula"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\nNot tested<\/p>\n
Import-module ActiveDirectory\n$users = Import-Csv \"C:\\ad_import\\users.csv\"\nforeach ($user in $users) {\n $props = @{\n identity = $user.samaccountname\n givenName = if($user.FirstName ){$user.FirstName }else{$null}\n surname = if($user.LastName ){$user.LastName }else{$null}\n title = if($user.JobTitle ){$user.JobTitle }else{$null}\n description = if($user.description){$user.description}else{$null}\n state = if($user.State ){$user.State }else{$null}\n Office = if($user.Office ){$user.Office }else{$null}\n department = if($user.Department ){$user.Department }else{$null}\n city = if($user.City ){$user.City }else{$null}\n manager = if($user.manager ){$user.manager }else{$null}\n company = if($user.company ){$user.company }else{$null}\n }\n set-aduser @props\n}\n\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nTested and works fine with me. Thanks a lot <\/p>","upvoteCount":0,"datePublished":"2017-09-06T19:27:27.000Z","url":"https://community.spiceworks.com/t/help-updating-ad-attributes-from-csv-file/604314/6","author":{"@type":"Person","name":"3abmula","url":"https://community.spiceworks.com/u/3abmula"}}]}}
3abmula
(Muhammad Abd Elmawla)
September 6, 2017, 6:53pm
1
I have a simple script
Import-module ActiveDirectory
$users = import-csv "C:\ad_import\users.csv"
foreach ($user in $users) {
set-aduser -identity $user.samaccountname -givenName $user.FirstName -surname $user.LastName -title $user.JobTitle -description $user.description -state $user.State -Office $user.Office -department $user.Department -city $user.City -manager $user.manager -company $user.company
}
It happens that some existing values in AD needs to be cleared for some users, so if I left a cell blank in the csv file I receive an error related to -replace parameter. I tried a lot but couldn’t fix it.
3 Spice ups
Neally
(Neally)
September 6, 2017, 7:08pm
2
Not tested
Import-module ActiveDirectory
$users = Import-Csv "C:\ad_import\users.csv"
foreach ($user in $users) {
$props = @{
identity = $user.samaccountname
givenName = if($user.FirstName ){$user.FirstName }else{$null}
surname = if($user.LastName ){$user.LastName }else{$null}
title = if($user.JobTitle ){$user.JobTitle }else{$null}
description = if($user.description){$user.description}else{$null}
state = if($user.State ){$user.State }else{$null}
Office = if($user.Office ){$user.Office }else{$null}
department = if($user.Department ){$user.Department }else{$null}
city = if($user.City ){$user.City }else{$null}
manager = if($user.manager ){$user.manager }else{$null}
company = if($user.company ){$user.company }else{$null}
}
set-aduser @props
}
1 Spice up
3abmula
(Muhammad Abd Elmawla)
September 6, 2017, 7:13pm
3
Hi JitenSh,
Maybe I wasn’t able to explain my requirements properly but I actually have some columns in the csv file has values for some users and empty for others, the empty values needs to clear an existing AD values if it does have value. so for example if I set -state $null that will clear State AD field for all users.
jitensh
(JitenSh)
September 6, 2017, 7:20pm
4
Muhammad Abd Elmawla:
Hi JitenSh,
Maybe I wasn’t able to explain my requirements properly but I actually have some columns in the csv file has values for some users and empty for others, the empty values needs to clear an existing AD values if it does have value. so for example if I set -state $null that will clear State AD field for all users.
empty value doesn’t mean anything,I didn’t say about all users only those users whom you want to clear some fields.
samaccountname,title,department
john, teamlead,IT
Nedd,accountnant,$null
3abmula
(Muhammad Abd Elmawla)
September 6, 2017, 7:26pm
5
JitenSh:
Muhammad Abd Elmawla:
Hi JitenSh,
Maybe I wasn’t able to explain my requirements properly but I actually have some columns in the csv file has values for some users and empty for others, the empty values needs to clear an existing AD values if it does have value. so for example if I set -state $null that will clear State AD field for all users.
empty value doesn’t mean anything, assume that you have csv I didn’t say about all users only those users whom you want to clear some fields.
samaccountname,title,department
john, teamlead,IT
Nedd,accountnant,$null
Unfortunately that wouldn’t help in my case because it’s an automated process and the csv file i receive from HR system in a specified share folder, and I have a scheduled task for the script runs daily. Thanks for your help anyway
3abmula
(Muhammad Abd Elmawla)
September 6, 2017, 7:27pm
6
Tested and works fine with me. Thanks a lot