[RESOLVED] String with escaped carriage returns/line breaks displays literally

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Paula Kingsley Paula Kingsley 5 months, 2 weeks ago.

  • Author
    Posts
  • #63717
    Profile photo of Paula Kingsley
    Paula Kingsley
    Participant

    NEVER MIND ... I had double-escaped the characters for some super important reason which escapes me now, and a keen-eyed person in technet pointed it out. I can't figure out how to delete this post, so please pretend it has self-destructed. 🙂

    —-
    Hi all –

    I'm trying to figure out why I'm seeing the literal output of a string containing escaped characters when I don't think I should be. (Long story short, I want to import a CSV file to create a slew of new AD groups, and the Notes field (-otherattributes @{info="foo"} will need to include line breaks).

    Much simplified version below.

    I import a CSV file...

    PS C:\> $CSVFile = "c:\temp\testimport.csv"
    PS C:\> $ImportCSV = Import-CSV -Path $CSVFile
    

    All goes well. Here's what it looks like:

    PS C:\> $ImportCSV
    
        GroupName Notes                                                         
        --------- -----                                                         
        Colors    Colors I hate: Red, Blue, Green; Colors I love: Orange, Yellow
        Animals   Mammals: Dogs, Cats, Marmosets; Birds: Canaries, Pigeons  
    

    I want to modify Notes to make it more readable:

    PS C:\> Foreach ($Group in $ImportCSV) {
        
        # Carriage return/new line after heading with colon
        If ($Group.Notes -match ":") {
            $Group.Notes = $Group.Notes -replace(":",":``r``n")
        }
        
        # Carriage return/new line before semicolon (next line is another heading)
        If ($Group.Notes -match ";") {
            $Group.Notes = $Group.Notes -replace("; ","``r``n")
        }
    
        # Carriage return/new line after comma (new sub-item)
        If ($Group.Notes -match ",") {
            $Group.Notes = $Group.Notes -replace(",","``r``n")
        }
    }
    

    And the output now looks like this:

    PS C:\> $ImportCSV
    
        GroupName Notes                                                                             
        --------- -----                                                                             
        Colors    Colors I hate:`r`n Red`r`n Blue`r`n Green`r`nColors I love:`r`n Orange`r`n Yellow
        Animals   Mammals:`r`n Dogs`r`n Cats`r`n Marmosets`r`n Birds:`r`n Canaries`r`n Pigeons
    

    Hmm; the Notes property still displays as a literal string with the escaped characters:

    PS C:\ ($ImportCSV | Select -first 1).Notes
    
        Colors I hate:`r`n Red`r`n Blue`r`n Green`r`n Colors I love:`r`n Orange`r`n Yellow
    

    However, if I copy that value to the clipboard and paste to a new variable, it displays just as I want:

    PS C:\> ($ImportCSV | Select -first 1).Notes | Clip
    PS C:\> $Test = "Colors I hate:`r`n Red`r`n Blue`r`n Green`r`nColors I love:`r`n Orange`r`n Yellow"
    PS C:\> $Test
        
        Colors I hate:
         Red
         Blue
         Green
        Colors I love:
         Orange
         Yellow
    

    I don't understand why the output is different. The types are the same.

    PS C:\> ($ImportCSV | Select -first 1).Notes.GetType()
    
        IsPublic IsSerial Name    BaseType                                                                          
        -------- -------- ----    --------                                                                          
        True     True     String  System.Object                                                                                            
                                  
    PS C:\> $Test.GetType()   
                                  
        IsPublic IsSerial Name    BaseType                                                                          
        -------- -------- ----    --------                                                                          
        True     True     String  System.Object     
    

    Halp?

    Thanks 🙂

You must be logged in to reply to this topic.