Add line break import-csv

Sam White 26 Reputation points
2021-03-04T09:31:25.48+00:00

I want to create some new users and add street address which a multi-line string. I have a csv file with the user name and street address. I want that when I add the users the Street and City are added on the new line and not added on the same line. How can I achieve this?
74228-testfile.png

Windows for business Windows Server User experience PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Sam White 26 Reputation points
    2021-03-04T09:38:52.723+00:00

    Currently the street address is added like: Street1 City1 so I want the City1 to appear on a new line.


  2. Rich Matheisen 47,901 Reputation points
    2021-03-04T15:02:57.307+00:00

    Something like this?

    Import-Csv C:\junk\x2.txt |
        ForEach-Object{
            $_.Address = $_.Address.Split(" ") -Join "`n"
            $_
        } |Export-Csv C:\junk\x2.csv -NoTypeInformation
    

    I have no idea what your real data look like so how you go about separating the street address from the city name is unknown.


  3. Anonymous
    2021-03-05T07:26:20.207+00:00

    Hi,

    Does this meet your needs?

    $file = 'D:\temp\users.csv'  
    $output = 'D:\temp\users2.csv'  
    $address = @'  
    {0}  
    {1}  
    '@  
    Import-Csv -Path $file | foreach-object {  
        $street, $city = $_.address -split ' '  
        $_.address =  $address -f $street, $city  
        $_  
    } | Export-Csv -Path $result -NoTypeInformation  
    

    If the street address and city address also have spaces, you have to separate them with some other symbol instead of the space.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.