I noticed the empty line separating the text blocks is not really empty, but it contains a space character.
One way of doing this is to use ConvertFrom-StringData:
$str = @"
Name : AdtAgent
DisplayName : Microsoft Monitoring Agent Audit Forwarding
Status : Stopped
StartType : Disabled
Name : AeLookupSvc
DisplayName : Application Experience
Status : Running
StartType : Automatic
"@
$result = $str -split '\r?\n\s*\r?\n' | ForEach-Object {
# for PowerShell 7 you can use
# $_ | ConvertFrom-StringData -Delimiter ':'
[PsCustomObject](($_ -replace ':', '=') | ConvertFrom-StringData)
}
$result
Output:
Status DisplayName StartType Name
------ ----------- --------- ----
Stopped Microsoft Monitoring Agent Audit Forwarding Disabled AdtAgent
Running Application Experience Automatic AeLookupSvc
This however does not keep the order of the properties if that matters, but also, if your values may contain a ':' character, this could give you bad results.
You can do this 'manually' of course like this, keeping the order and not getting confused if values have a colon character:
$result = $str -split '\r?\n\s*\r?\n' | ForEach-Object {
$hash = [ordered]@{}
foreach ($line in ($_ -split '\r?\n')) {
$name, $value = ($line -split ':', 2).Trim()
$hash[$name] = $value
}
# cast to PsCustomObject for output
[PsCustomObject]$hash
}
$result
Output:
Name DisplayName Status StartType
---- ----------- ------ ---------
AdtAgent Microsoft Monitoring Agent Audit Forwarding Stopped Disabled
AeLookupSvc Application Experience Running Automatic
Format-List, in which case you already have objects and there would be no need to convert from string back to objects. Please share the code that produces this output.