Turn fullname into AD name

Hey guys,

So im stuck here. I’ve done this a bunch of times in SQL but never in PS. I have a full name like “John Smith” that i am writing a script to auto change password when its expired. However, AD does not like “John Smith” so i need to change it to “JSmith”.
In the past for SQL ive used a combination of charindex, length, substring etc etc. But for the life of me i am drawing a blank on how to do this in PS. Any help would be great!

Thank you

This example should work.

$user = “Joe Smith”
$user.Substring(0,1)+$user.Split(" ")[1]

Thank you soooo much. This works great!

Hi,

Are you trying to change the name in AD?
Can’t you just use:

$User = Get-ADUser -identify John.Smith
Set-ADUser -Identity $User -UserPrincipalName JSmith 

Sorry if im not understanding the question?

Sorry, i’ve just now understood what you are doing.
As rick said :slight_smile:

I frequently get a list of users that need permission to whatever in the form of display name. So I dump the display names in a text file and do this:

gc C:\scripts\displaynames.txt | % {Get-ADUser -LDAPfilter “(DisplayName=$_)”} | select samaccountname