1

I have a text file in this format:

ServerName:srvrX1
IP:
Location:NY
OS:

ServerName:srvrX2
IP:
Location:NY
OS:Windows Server 2012
XYZ:

ServerName:srvrX3
IP:
Location:STK
OS: Win2k3

ServerName:srvrX4
IP:
Location:STK
OS:Win2k8

ServerName:srvrX5
IP:
Location:STK
OS: RedHat Linux 5

ServerName:srvrX6
IP:
Location:TKY
OS: Windows Server 2012

The number of fields could vary for each server and the number of empty lines between each server info is randm . I want the output in tabular format. I am new to powershell could someone please help.

Thanks a lot. Nel

1 Answer 1

6

You can split the content on two or more line breaks, then split each returned element again, parse the content and create new object for each element:

$content = Get-Content .\test.txt | Out-String

$content -split '(?:\r\n){2,}' | Foreach-Object{

    $pso = New-Object PSObject

    $_ -split '(?:\r\n)' | Where-Object {$_} | ForEach-Object{

        $var = $_ -split ':'
        $pso | Add-Member -MemberType NoteProperty -Name $var[0].Trim() -Value $var[1].Trim()
    }

    $pso

} 


ServerName IP Location OS                 
---------- -- -------- --                 
srvrX1        NY                          
srvrX2        NY       Windows Server 2012
srvrX3        STK      Win2k3             
srvrX4        STK      Win2k8             
srvrX5        STK      RedHat Linux 5     
srvrX6        TKY      Windows Server 2012
Sign up to request clarification or add additional context in comments.

1 Comment

@user2764554, you should accept this as the answer, especially since you said it worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.