Search code examples
powershellactive-directoryline-breaks

Powershell - Line break in 'Notes' field of an AD object


I've set up a script for automatic user creation in our AD. Everything is working beautifully, except for one thing. The Notes field in the index tab Telephones of an AD object seems to ignore any line breaks added in Powershell.

$notes = "E-Mail: $mail`nPhone: $phone`nInternal Contact:$contact"

#random irrelevant code in between

OtherAttributes   = @{
        'info' = $notes
}

OtherAttributes is part of a hashtable with other AD user parameters as well.

Screenshots of 1) how it is and 2) how it should be.

How it is

enter image description here

Is this even possible?

It doesn't have any issues when doing it manually, so I'm assuming there has to be a way for it to work via script.


Solution

  • If you want to create the notes in a single double-quoted line, then use Windows newlines (CRLF) like Mathias already commented:

    $notes = "E-Mail: $mail`r`nPhone: $phone`r`nInternal Contact: $contact"
    

    or fill these in with the -f Format operator:

    $notes = "E-Mail: $mail{0}Phone: $phone{0}Internal Contact: $contact" -f [Environment]::NewLine
    

    Perhaps an even better approach would be to use a Here-String:

    $notes = @"
    E-Mail: $mail
    Phone: $phone
    Internal Contact: $contact
    "@