First powershell script - what causes object to display?


I have written my first PS script and very excited to be using the tool. I was able to combine two objects and mostly it works, but I when I run it, the Combined Object displays on each loop. I am curious which line of code causes this. Also, it appears my final Combined Object is only a single row and not the rows from the file.

I appreciate all critiques, this is the way we learn.

Thank you.

function Merge-Objects {
    param (
        [Object] $Object1,
        [Object] $Object2
    $global:CombinedObject = [ordered] @{}
    foreach ($Property in $Object1.PSObject.Properties) {
        $CombinedObject += @{$Property.Name = $Property.Value}

    foreach ($Property in $Object2.PSObject.Properties) {
        $CombinedObject += @{$Property.Name = $Property.Value}
    return [pscustomobject]$CombinedObject
# Main processing routine
#Import the Netsuite file into object
Write-Host "Import $netsuite_file"
$netsuite_rows = Import-Excel $report_directory\$netsuite_file  -ImportColumns @(2, 3, 5, 13, 14, 15, 16, 20) -StartRow 1 -EndRow 1000
#Import the Magento file into object
Write-Host "Import Magento file"
$magento_rows = Import-Excel $report_directory\$magento_file  -ImportColumns @(1, 8, 9, 22, 24, 26, 47, 77) -StartRow 1 -EndRow 1000
#Loop through the Netsuite rows and query the Magento rows for the item
foreach ($nsrow in $netsuite_rows) {
    if ($nsrow.Name -like "*.00*") { #If the item ends in .00, just search for the first 5 characters
        $searchfor = $nsrow.Name.Substring(0,5)
    } else {
        $searchfor = $nsrow.Name
         foreach ($mgrow in $magento_rows)
          $mgrow | Where-Object -Property sku -Like $searchfor |  
             Foreach-Object {
              #Test add values to object
              $newObject = $mgrow | Select-Object *, @{label='CA Prop 65 Warning'; expression={'No'}}
              #Test remove values from object
              #$object = $newObject | Select-Object * -ExcludeProperty ID, Address
              #Test modify the value of an object
              $newObject | ForEach-Object {$_.'thumbnail_image' = "Mickey Mouse"
              Merge-Objects -Object1 $nsrow -Object2 $newObject
        #future - create the Excel file and output CombinedObject
        #rm $report_directory$output_file
        #Export-Excel @ExcelParams
        #$CombinedObject | Export-Excel $report_directory$output_file -Append -WorksheetName "Sheet1"
        #$CombinedObject  - for testing, only shows one element?

Welcome to the forum. :wave:t4:

Before we proceed … coulöd you please go back, edit your question and format your code as code using the preformatted text button ( </> )? Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:


Thank you. I am sorry for my error. It is fixed. Thanks for the help.


1 Like