I am working on powershell code to check each column in all rows in the excel spreadsheet and check if any line breaks are exist and convert them into commas. I have tried with below code but it is not updating the line breaks into commas.
Code:
# Load the Excel file
$path = "./NSG_Rules.xlsx"
$exceldata = Import-Excel -Path $path
# Iterate through each cell and replace line breaks with commas if present
`foreach ($row in $exceldata) { foreach ($property in $row.psobject.Properties) { # Check if the cell content has line breaks and replace them with commas if ($property.Value -match "`r`n") { $property.Value = $property.Value -replace "`r`n", "," } } }`
# Save the updated workbook.
$rows | Export-Excel $exceldata
My requirement is, I have excel spread sheet and it is having many rows and columns. the powershell code needs to check each cell in the excel spread sheet and if any line breaks(refer screenshot) found then it needs to convert it into commas and update the excel sheet.
In the uploaded image screenshot, the highlighted red color cell is having the line break. so it needs to convert into single line with comma like below: I have tried below code but it is not updating the excel spread sheet by removing the line breaks and converting them into comma.
Screenshots for reference:
Present format:
Required outputformat:

As mclayton points out:
you're not dealing with newlines (line breaks) inside cells (if you were, this answer would help you).
you're dealing with "overflow rows", where what is conceptually an additional cell value is actually contained in a separate row, i.e. in an additional cell below the cell of interest (with the other cells in a row of interest merged with their empty counterparts below).
To append these "overflow values" to their corresponding main-row values, you can use the following approach:
Process rows conditionally, depending on whether they are "main" or "overflow" rows.
Detect the latter by whether their first cell is empty.
Among the remaining cells, append any non-empty values to their main-row counterparts.
Important:
In order to be able to save changes to a workbook, it mustn't currently be open in Excel. You can ensure this manually or - assuming you're comfortable doing so - can automatically forcefully terminate any running Excel processes to ensure that saving succeeds, by adding the
-KillExcelswitch to theExport-Excelcalls below.The solution below uses
Export-Excel's-ClearSheetswitch to re-create the worksheet's data, which has two implications:All formatting is lost; see the next section for a - more cumbersome - solution that preserves formatting.
If the workbook happened to have multiple sheets, the re-created sheet is placed last (arguably, this shouldn't happen); the
-MoveToStartswitch used below compensates for that.Not using
-ClearSheetisn't an option (without substantial additional effort that may require use of theExcel.ApplicationCOM Automation server instead), because:Fundamentally,
Export-Exceldoesn't replace a worksheet's content, but simply updates it, with any preexisting formatting retained as-is, as well as any content unaffected by the update.While you could - somewhat - compensate for the reduction in the count of rows due to the described processing - by appending additional, empty, rows that blank out the extra original ones, which requires passing objects that contain empty strings for each target column - merged cells present in the original worksheet would then effectively hide values. See the next section for a solution that requires more work.
Solution that preserves formatting:
Such a solution requires a two-pass approach:
First, update the cells in the "main" rows only; leave the "overflow" rows alone, but record their row indices in a list for later removal.
Then, via
Export-Excel -PassThru- which is equivalent to callingExport-Excel(to write the updated values) followed byOpen-ExcelPackage- obtain a reference to the Excel package (workbook) for advanced operations, which allows removal (deletion) of rows directly from the spreadsheet.