Change XML file value with excel file value through power shell script
Change XML file value with excel file value through power shell script
Script:
# Load the Excel PowerShell module Import-Module -Name ImportExcel # Path to the Excel file $excelFilePath = "C:\Users\Administrator\Desktop\Test.xlsx" # Path to the XML file $xmlFilePath = "C:\Users\Administrator\Desktop\SalesTable.SizeWiseYield.xml" # Load the Excel file $excelData = Import-Excel -Path $excelFilePath # Read the content of the XML file $xmlContent = Get-Content -Path $xmlFilePath # Iterate through each cell in the Excel file foreach ($row in $excelData) { foreach ($cell in $row.PSObject.Properties) { $cellValue = $cell.Value # Construct the pattern to match <Name>CellValue</Name> $pattern = "<Name>$cellValue</Name>" # Replace the value in the XML file with "DS_CellValue" if it matches $xmlContent = $xmlContent -replace [regex]::Escape($pattern), "<Name>DS_$cellValue</Name>" } } # Write the modified content back to the XML file $xmlContent | Set-Content -Path $xmlFilePath
Comments
Post a Comment