How to remove spaces of composed lastname ?

Hello,

I have to create AD users from a CSV and would like to know how to remove spaces of a composed last name, for example if an employee's full name is Neymar, Da Silva, then username will be dasilvan

Thanks for your help,
namergSystems AdministratorAsked:
Who is Participating?
 
SubsunConnect With a Mentor Commented:
"Da Silva" -replace "\s",""
Should give you.. DaSilva

With csv you ca try.. $_.lastname -replace "\s",""
0
 
Raheman M. AbdulConnect With a Mentor Senior Infrastructure Support Analyst & Systems DeveloperCommented:
$firstname=" abdul "
$lastname="rahman mohammed"

$firstname=$firstname -replace "\s",""

$lastname=$lastname -replace "\s",""


$username = $lastname+($firstname[0])

--------------------

#Sample output is:

rahmanmohammeda
0
 
namergSystems AdministratorAuthor Commented:
But, how do i validate it...?
if lastname does not have space
$lastname=$lastname -replace "\s",""  would not matter ?
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
does not matter
0
 
SubsunCommented:
Yes it doesn't matter. If there is no space then you will get the same input string as a result..
0
 
namergSystems AdministratorAuthor Commented:
Will the following
$LastName = $_."Last Name" -replace "\s"

Open in new window

work ?
0
 
SubsunCommented:
Yes..

$LastName = $_."Last Name" -replace " "

Will also work..
0
 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
what is $_ representing?
0
 
namergSystems AdministratorAuthor Commented:
Before:
$sam = $_."Last Name".ToLower() + $_."First Name".substring(0,1).ToLower()
After:
$LastName = $_."Last Name" -replace "\s"
$sam = $LastName.ToLower() + $_."First Name".substring(0,1).ToLower()
0
 
SubsunCommented:
That should do it..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.