I am preparing one PowerShell script which needs to check If values in one cell are delimited by line breaks, change them to commas and update the designsheet (excelsheet). I am using below code for the same and it is executing without any errors but it is directly giving the output as "No changes made to the Excel file" and design sheet is also not updated. I have tried multiple ways but still facing the issue. Can some one please help me to resolve this issue. Below is the code:
# Load the Excel file
$designSheet = "/home/lokeshdir/NSG_Rules.xlsx"
$excel = Import-Excel -Path $designSheet
# Flag to track changes
$changesMade = $false
# Iterate through each cell and replace line breaks with commas if present
foreach ($row in $excel) {
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", ","
$changesMade = $true # Set flag indicating changes
}
}
}
# If changes were made, save the updated Excel file
if ($changesMade) {
$excel | Export-Excel -Path $designSheet -Show
Write-Host "Excel file updated successfully."
} else {
Write-Host "No changes made to the Excel file."
}
Output:
PS /home/lokeshdir> ./NSGRulesadd.ps1
No changes made to the Excel file.
PS /home/lokeshdir>
Can someone please help me to resolve this issue to check the design sheet if any line breaks present and change those line breaks to commas and update the design sheet.

Update:
Closer examination of the screenshot added later suggests that it isn't actually line breaks (newline) inside your cells that are the problem, but values that are conceptually single ones being spread across two cells - a solution to the latter problem can be found in this answer.
The answer below addresses the question as asked.
Perhaps surprisingly, Excel converts Windows-format CRLF newlines (
"`r`n") to Unix-format LF-only newlines ("`n") on assigning multiline strings to cells, irrespective of whether values are entered interactively or set programmatically, as verified with v7.8.6 of theImportExcelmodule, both with a programmatically created workbook and one created interactively via Office 2019.[*]Therefore, simply replacing
"`r`n"with"`n"in your code should solve your problem.As an aside: what Theo suggests -
"`r?`n"- is a regex that matches either newline form, more typically expressed via regex escape sequences,'\r?\n', but it isn't necessary here.Self-contained sample code:
The following:
uses
Export-Excelto create sample workbooksample.xlsxwith a single data row with two multiline values, one with a CRLF-only newline ("line1`r`nline2"), and another with a LF-only newline ("20`n21").reads the file back in with
Import-Exceland verifies that the CRLF newline turned into a LF-only ("`n") newline.replaces the now-LF-only newlines with
","and saves the workbook, then re-imports it to verify that the changes were performed as intended, which is indeed the case for me with version 7.8.6 of theImportExcelmodule.Export-Excelexports even string values that look like numbers as numbers, which can be undesired."20`n21"into"20,21"would result in interpretation of the latter as a number, namely as2021(the,is interpreted as a thousands-grouping symbol).-NoNumberConversion <propertyNames>parameter suppresses this behavior;-NoNumberConversion *can be used to apply it to all exported properties (columns).However, the latter would then format even actual numbers as strings, so it is better to only specify affected properties, as done below.
You should see the following output - note how only LF-only newlines are reported, and how the multiline values were converted to
,-separated single-line ones:[*] If you have information about different behavior in earlier or later versions, please let us know.