Turn fullname into AD name

This topic contains 5 replies, has 4 voices, and was last updated by  Jon 3 weeks, 5 days ago.

  • Author
    Posts
  • #80378

    David
    Participant

    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

  • #80381

    Rick
    Participant

    This example should work.

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

  • #80384

    David
    Participant

    Thank you soooo much. This works great!

  • #80386

    TommyQuality
    Participant

    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?

    • #80387

      TommyQuality
      Participant

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

  • #80401

    Jon
    Participant

    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

You must be logged in to reply to this topic.