Sometimes when you work with objects in Powershell and want to concatenate field as output you need to get rid of spaces. Below is an example on how to deal with that.

The cmdlets below import records from the file C:\CSV-files\Userlist.txt, filters on Achternaam = “Zwart” and shows the content of the variable $Users.

PS H:\> $Users = Import-Csv ‘C:\CSV-files\Userlist.txt’ -Delimiter “;” | Where-Object {$_.Achternaam -eq “Zwart”} PS H:\> $Users Voornaam Achternaam Email ——– ———- —– Willem Zwart willems@mydomain.eu

If you want to output both Achternaam and Voornaam combined one could use the following cmdlet. But notice the space between “Zwart” and the comma.

PS H:\> Write-Host $Users.Achternaam”,”$Users.Voornaam Zwart , Willem

To get rid of that space an easy solution is to fill two new variables with the ouput of the object’s $Users.Achternaam and $Users.Voornaam and combine these two to output them to the screen.

Notice that the space between “Zwart” and the comma has gone.

PS H:\> $Voornaam = $Users.Voornaam PS H:\> $Achternaam = $Users.Achternaam PS H:\> Write-Host “$Achternaam, $Voornaam” Zwart, Willem