Search code examples
powershellpowershell-5.0

powershell - mystery whitespace from output of loops


I am getting whitespace added to my output from the following section of code. I need to format this a certain way so for sake of finding out where this whitespace is coming from I am just outputting the variables. I even added .trim() to make sure it wasn't coming from the variables themselves. Where in the heck is this whitespace coming from?

#sort by Name and sort Values of each
$output += foreach($icon in $table.GetEnumerator() | sort -Property Name) {
    $icon.Name.trim()
    foreach($type in $icon.Value | sort) {
        $fa_types[$type].trim()
    }
}

#Output to file
"version: " + $fa_version + "`r`nicons:`r`n" + $output | Out-File $output_file

example output file :

version: 5.13.0
icons:
arrow-circle-right solid regular calendar-week regular users solid usb-drive solid file-alt regular key solid user-graduate solid comment-dots regular plus solid calendar-check regular spinner regular stopwatch regular file-search solid user-chart solid map-marker-alt regular calculator regular apple brands 

Running powershell version 5 on Windows 10.


Solution

  • That's a strange way to create a string... I recommend a safer way, where no output functions of PowerShell are involved:

    #sort by Name and sort Values of each
    $output = ""
    foreach($icon in $table.GetEnumerator() | sort -Property Name) {
        $output += $icon.Name
        foreach($type in $icon.Value | sort) {
            $output += $fa_types[$type]
        }
    }
    
    #Output to file
    "version: " + $fa_version + "`r`nicons:`r`n" + $output | Out-File $output_file