Author Posts

September 22, 2017 at 1:12 pm

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

September 22, 2017 at 1:51 pm

This example should work.

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

September 22, 2017 at 2:11 pm

Thank you soooo much. This works great!

September 22, 2017 at 2:16 pm

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?

September 22, 2017 at 2:18 pm

Sorry, i've just now understood what you are doing.
As rick said 🙂

September 22, 2017 at 6:03 pm

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