Sanity check on code please

Alex
Alex used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

AlexSenior Infrastructure Analyst

Author

Commented:
So splatting is very very similar to a hash group?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.
AlexSenior Infrastructure Analyst

Author

Commented:
That's pretty. Very pretty. oBdA you need to let me buy you a few drinks.... :-)
Juan OcasioApplication Developer

Commented:
splatting creates a hash table of key/value pairs which you can chain onto a cmdlet.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial