ToTitleCase variable with New-ADUser

Welcome Forums General PowerShell Q&A ToTitleCase variable with New-ADUser

This topic contains 3 replies, has 2 voices, and was last updated by

1 year, 6 months ago.

  • Author
  • #76710

    Points: 2
    Rank: Member


    I have been trying to convert an all caps string to title case, and it works, but not as part of a New-ADUser routine.

    If I do not include the conversion, the script works fine. Here is part of a ForEach-Object loop...

    $params = @{
    $preferredname = (Get-Culture).TextInfo.ToTitleCase($_.PreferredName.ToLower())
    $lastname = (Get-Culture).TextInfo.ToTitleCase($_.LastName.ToLower())
                        'Name' = $preferredname + ' ' + $lastname
                        'Surname' = $lastname
                        'DisplayName' = $_.DisplayName
                        'GivenName' = $preferredname
                        'EmailAddress' = $_.EmailAddress
                        'SamAccountName' = $_.UserLogonName
                        'UserPrincipalName' = ($_.UserLogonName + '@domain')
                        'Title' = ($_.WorkDuties)
                        'Department' = ($_.Loc_Name)
                        'HomeDrive' = "H:"
                        'HomeDirectory' = ($_.HomeServer + '\HOME\' + $_.UserLogonName)
                        'Path' = $path
                        'AccountPassword' = (ConvertTo-SecureString "$($genPass)" -AsPlainText -force)
                        'ChangePasswordAtLogon' = $true
                        'Enabled' = $true
                        'OtherAttributes' = @{'Info'="$($genPass)"}
                        Try {New-ADUser @params -ErrorAction Continue
                             $successUsers += "$($_.PreferredName) $($_.LastName) - $($_.WorkDuties) at $($_.Loc_Name). $($genPass) `r`n"}
                        Catch {$usersErr += "$($_.Name) - $($_.WorkDuties) at $($_.Loc_Name). `r`n"} 
                        } -ErrorAction SilentlyContinue -ErrorVariable +usersErrException

    Doing this causes the Try New-ADUser to fail. Removing this and just using 'Surname' = $_.LastName, for example, works.

  • #76747

    Points: 0
    Rank: Member

    What error messages do you get?
    have you checked the contents of $preferredname and $lastname?

  • #76750

    Points: 0
    Rank: Member

    Try moving these 2 lines
    $preferredname = (Get-Culture).TextInfo.ToTitleCase($_.PreferredName.ToLower())
    $lastname = (Get-Culture).TextInfo.ToTitleCase($_.LastName.ToLower())

    outside of the hashtable

    • #76846

      Points: 2
      Rank: Member

      Before posting this here, I had already tried moving those 2 lines just above the hashtable. It didn't work.

      After your suggestion I started to wonder why it shouldn't, so I tried again this morning and it worked fine.

      I am not sure what I did before, but it works.


The topic ‘ToTitleCase variable with New-ADUser’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort