Sanity check on code please

Guys,

Can I please get a sanity check on this?

# Import AD Module             
Import-Module ActiveDirectory            
                        
#enter CSV location
             
$users = Import-Csv -Path 'C:\temp\source.csv'         
# Loop through CSV and update users if the exist in CSV file            
            
foreach ($user in $users) {            
#Search in specified OU and Update existing attributes            
             
     Set-ADUser -identity $user -EmployeeID $user.EmployeeID -Department $User.'Department Name' -Company $user.Company -Office $user.Office -title $user.'Job Title'

Open in new window


Thanks
Alex
LVL 18
Alex GreenProject Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
I'd use splatting for the arguments (makes it easier to react if some attributes requires special attention, for example if they may be empty in the csv), and you didn't specify the property to be used as Identity for Set-ADUser (I'm assuming SamAccountName below).
# Import AD Module             
Import-Module ActiveDirectory            
                        
#enter CSV location
$users = Import-Csv -Path 'C:\temp\source.csv'         
# Loop through CSV and update users if the exist in CSV file            
$users | ForEach-Object {            
	$splat = @{
		EmployeeID = $_.EmployeeID
		Department = $_.'Department Name'
		Company = $_.Company
		Office = $_.Office
		Title = $_.'Job Title' 
	}
	Set-ADUser -Identity $_.SamAccountName @splat
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex GreenProject Systems EngineerAuthor Commented:
So splatting is very very similar to a hash group?
0
oBdACommented:
It is a hash table, with the argument names as keys, and the value for the argument as value ([switch] types can be handled like bool).
The "splatting" part comes when passing it to the function/cmdlet: note the @ (instead of the $) in front of the variable name containing the hash table.
And as you can see above, you can combine splatting and regular arguments without problem.
splatting is usually easier to read/handle than line continuation with a near-invisible backtick.
1
Alex GreenProject Systems EngineerAuthor Commented:
That's pretty. Very pretty. oBdA you need to let me buy you a few drinks.... :-)
0
Juan OcasioApplication DeveloperCommented:
splatting creates a hash table of key/value pairs which you can chain onto a cmdlet.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.