Score:0

How do I create an AD username with a dot in it via Powershell?

kr flag

I'm trying to change an existing script so that my username is firstname intial dot last name for example: John Doe's username will be j.doe Current script works (without the .) as: $firstname.substring(0,$i) + $lastname

thank you.

br flag
the simplest way - for me - is to use the `-f` string format operator. like this >>> `'{0}.{1}' -f 'John'[0], 'Doe'` <<< [*grin*]
Score:2
br flag

there are several ways to build that string. [grin] here are 4 of them that come to mind for me.

what the code does ...

  • fakes reading in a CSV file with FirstName, LastName data
    replace the entire #region/#endregion block with your preferred data source.
  • iterates thru the resulting collection
  • builds the desired string with 4 different methods
    my preference is the -f string format operator, but many folks prefer string concatenation.
  • sends each out to the display
  • adds a divider line between the groups of results

the code ...

#region >>> fake reading in a CSV file
#    when ready to do this for real, use your prefered data source
#    and delete or comment out the entire "#region/#endregion" block
$NameList = @'
FirstName, LastName
Alfa, Bravo
Charlie, Delta
Echo, Foxtrot
'@ -split [System.Environment]::NewLine |
    ConvertFrom-Csv
#endregion >>> fake reading in a CSV file

foreach ($NL_Item in $NameList)
    {
    # string format operator
    '{0}.{1}' -f $NL_Item.FirstName[0], $NL_Item.LastName

    # -join operator
    $NL_Item.FirstName[0], $NL_Item.LastName -join '.'

    # string concatenation
    $NL_Item.FirstName[0] + '.' + $NL_Item.LastName

    # variable-in-string expansion
    "$($NL_Item.FirstName[0]).$($NL_Item.LastName)"

    # yes, you can multiply a string [*grin*] 
    '=' * 20
    }

output ...

A.Bravo
A.Bravo
A.Bravo
A.Bravo
====================
C.Delta
C.Delta
C.Delta
C.Delta
====================
E.Foxtrot
E.Foxtrot
E.Foxtrot
E.Foxtrot
====================
Score:1
es flag
$firstname.substring(0,$i) +"."+ $lastname
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.