Import-CSV into AD - how do I add a space?

by NeatAccurateTypist at 2013-03-10 11:50:56

I’m trying to amend a really useful command from page 82 in the Powershell in a Month of Lunches book. The command imports a list of users from a CSV file and then creates ‘labels’ matching AD properties so the data can be assigned. The book states that this is useful when your HR department sends you a spreadsheet of users but the supplied column headings (i.e. ‘FirstName’ or ‘LastName’) don’t correspond with their respective AD properties (i.e ‘GivenName’ and ‘Surname’) and therefore the data can’t be imported directly.


Import-CSV c:\users2.csv |
? Select-Object *,@{l=‘samAccountName’;e={$.LoginName}},
? @{l=‘Name’;e={$
.LoginName}},
? @{l=‘GivenName’;e={$.FirstName}},
? @{l=‘Surname’;e={$
.LastName}}


My problem is that I want to change the ‘Name’ property (2nd hashtable) so that it displays first name and last name for the AD account, something like this:


Import-CSV c:\users2.csv |
? Select-Object *,@{l=‘samAccountName’;e={$.LoginName}},
? @{l=‘Name’;e={$
.FirstName+$.LastName}},
? @{l=‘GivenName’;e={$
.FirstName}},
? @{l=‘Surname’;e={$.LastName}}

I can’t seem to put a space in between the first name and last name values though. I’ve tried a few things, including inserting a plus symbol as above, but that doesn’t seem to do anything. Therefore, the AD account name comes up as ‘JoeBloggs’ rather than ‘Joe Bloggs’

I’m sure it’s something really simple that I’ll probably find a bit further on in the book, but I need to do an AD import this week! If anyone has the answer I’ll be really grateful!
by mjolinor at 2013-03-10 12:07:18
One way is to use an expandable string, with subexpressions

{l='Name';e={"$($
.FirstName) $($_.LastName)"}},
by NeatAccurateTypist at 2013-03-10 12:20:02
That worked perfectly first time! Thank you so much mjolinor - you’re a legend! :slight_smile: