0

I want to convert the Text File to CSV File and below is the Format using Powershell and need some assistance

  • Text File Structure: This is how the data looks in output or text file enter image description here

Content for reference:

Quantity: 30
Manufacturer: Microsoft
License metric: Microsoft Core Infrastructure Server Suite Datacenter Core
Expiry: 31/12/2024

Quantity: 70
Manufacturer: Microsoft
License metric: Microsoft Windows Server Datacenter Core (SA)
Expiry: 31/12/2025

Quantity: 58
Manufacturer: Microsoft
License metric: Microsoft Core Infrastructure Server Suite Datacenter Core
Expiry: 31/12/2024

Convert Text File to CSV (into rows and columns) CSV Header: Quantity, Manufacturer, License metric, Expiry

2
  • Please read How to Ask and how to provide a minimal reproducible example. Right now you only provided a set of requirements. Commented Jun 2, 2024 at 23:43
  • @DavidMakogon: While it is always preferable for a question to demonstrate a solution attempt, it isn't strictly necessary if the problem is fully specified - as in the case at hand. At the end of the day - assuming the problem is of general interest - the community still benefits if there are answers with working solutions - future readers won't care if the question itself contains a solution attempt. Thus, if a question already has an accepted answer / answers with working solutions, there is no point in retroactively insisting the OP demonstrate a solution attempt. Commented Jun 3, 2024 at 2:48

3 Answers 3

2

Note:


I suggest processing your file via the regex-based -split operator:

# Create an ordered helper hashtable to help with [pscustomobject] construction
$oht = [ordered] @{} 

# Split the input file into blocks of lines, and process each block.
(Get-Content -Raw file.txt) -split '\r?\n\r?\n' -ne '' | ForEach-Object { 
  $oht.Clear() # Clear the helper hashtable to start constructing a new object.
  # Split the block at hand into individual lines and process each.
  $_ -split '\r?\n' -ne '' | ForEach-Object {
    # Split the line at hand into property name and value...
    $name, $value = $_ -split ':', 2
    # ... and add this property-name pair to the helper hashtable.
    $oht[$name] = $value.Trim()
  }
  # Construct a [pscustomobject] from the helper hashtable and output it.
  [pscustomobject] $oht
}  # | Export-Csv ...  # Add your Export-Csv call here.

Display output, showing the constructed objects and their properties, using implicit table formatting:

Quantity Manufacturer License metric                                             Expiry
-------- ------------ --------------                                             ------
30       Microsoft    Microsoft Core Infrastructure Server Suite Datacenter Core 31/12/2024
70       Microsoft    Microsoft Windows Server Datacenter Core (SA)              31/12/2025
58       Microsoft    Microsoft Core Infrastructure Server Suite Datacenter Core 31/12/2024

Note:

  • This solution works with any similarly formatted text file (where each contiguous block of lines represents an object, and property names and values are separated by :, and are unquoted), irrespective of the specific property names.

  • Due to using regex \r?\n to match newlines (line breaks), both Windows-format files with CRLF newlines and Unix-format files with LF-only newlines are supported.

  • For each block of lines, an ordered hashtable ([ordered] @{}) is used to collect that block's property-value pairs.

  • Casting this hashtable to [pscustomobject] creates an instance of the latter, suitable for piping to Export-Csv.

Sign up to request clarification or add additional context in comments.

Comments

2

If the order of the properties is not a concern, you could take advantage of ConvertFrom-StringData to simplify this task. Github issue #19070 proposes this issue be fixed.

For powershell 6+ which offers the -Delimiter parameter we can process as is

# Get all the text data in one string
$data = Get-Content textfile.txt -Raw

# split on repeating new lines to get the blocks of text
$blocks = $data -split '\r?\n\r?\n'

# process each block 
$blocks | Foreach-Object {
    # cast the resulting hash table to pscustomobject
    [PSCustomObject]($_ | ConvertFrom-StringData -Delimiter ':')
}

For powershell 5.1, we have to replace the colon with an equals sign

# Get all the text data in one string
$data = Get-Content textfile.txt -Raw

# split on repeating new lines to get the blocks of text and replace the first found colon on each line with an equals character
$blocks = $data -split '\r?\n\r?\n' -replace '(?m)(?<=^[^:]+):','='

# process each block 
$blocks | Foreach-Object {
    # cast the resulting hash table to pscustomobject
    [PSCustomObject]($_ | ConvertFrom-StringData)
}

Both should produce this output

License metric                                             Expiry     Quantity Manufacturer
--------------                                             ------     -------- ------------
Microsoft Core Infrastructure Server Suite Datacenter Core 31/12/2024 30       Microsoft
Microsoft Windows Server Datacenter Core (SA)              31/12/2025 70       Microsoft
Microsoft Core Infrastructure Server Suite Datacenter Core 31/12/2024 58       Microsoft

Comments

0

To convert the text file to CSV file, you can use the following PowerShell script:

$inputFile = "file1.txt"
$outputFile = "fileout1.csv"
$inputLines = Get-Content $inputFile
$outputLines = @("Quantity,Manufacturer,License metric,Expiry")
for ($i = 0; $i -lt $inputLines.Length; $i += 4) {
    $quantity = $inputLines[$i] -replace "Quantity: "
    $manufacturer = $inputLines[$i+1] -replace "Manufacturer: "
    $licenseMetric = $inputLines[$i+2] -replace "License metric: "
    $expiry = $inputLines[$i+3] -replace "Expiry: "
    $outputLines += "$quantity,$manufacturer,$licenseMetric,$expiry"
}
$outputLines | Out-File $outputFile

Make sure to replace the $inputFile and $outputFile variables with the actual paths to your input and output files. This script reads the input file, splits the lines into columns, and writes the output to a CSV file with the specified header.

Output:

Quantity,Manufacturer,License metric,Expiry
30,Microsoft,Microsoft Core Infrastructure Server Suite Datacenter Core,31/12/2024
70,Microsoft,Microsoft Windows Server Datacenter Core (SA),31/12/2025
58,Microsoft,Microsoft Core Infrastructure Server Suite Datacenter Core,31/12/2024

4 Comments

Thanks Aswin for sharing but looks like not working for me: Output below: Quantity,Manufacturer,License metric,Expiry 30,Microsoft,Microsoft Core Infrastructure Server Suite Datacenter Core,31/12/2024 ,Quantity: 70,Manufacturer: Microsoft,License metric: Microsoft Windows Server Datacenter Core (SA) Expiry: 31/12/2025,,Quantity: 58,Manufacturer: Microsoft License metric: Microsoft Core Infrastructure Server Suite Datacenter Core,Expiry: 31/12/2024,,
Header and First record shows fine but it adds Quantity, Manufacturer etc. with the data from second line in output
Not sure, why header is coming along with every row in your output. I got the output as your expectation. Refer the screenshot in the answer. I will check in my environment again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.