Updating CSV cells

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by  fpoli 2 weeks, 6 days ago.

  • Author
    Posts
  • #78865

    Andy Smith
    Participant

    Afternoon all.

    I'm trying to update csv cells after finding the right one.. It works on small files but my sheet is 15,000 rows and my script hangs..
    Is there anyway to improve it?

    function bookINGEM {
    
    $Eng = (get-qaduser $user).displayName
    $DelDate = (Get-Date -format d)
    $Refe = $SerialRef.text
    $file = "C:\temp\Tracker.csv"
    
    $csv = Import-Csv $file 
    
    
    foreach($row in $csv) 
    {
    if ($row.SerialNo -eq $Refe) {
        $row.HostName = $BoxHost.text
        $row.Asset = $BoxAsset.text
        $row.Location = "LOCXXXXXX"
        $row.LifeCycleStatus = "In Stock"
        $row.SubStatus = "Available"
        $row.Date = $DelDate
        $row.Updated = $Eng   
    }
    
    $csv | Export-Csv  $file -NoTypeInformation;
    }
    $StatusBarPanel.text = "Updated"
    }
    
  • #78868

    fpoli
    Participant

    Hi Andy,
    just change the code from:

    }
    
    $csv | Export-Csv  $file -NoTypeInformation;
    }
    

    to:

    }
    }
    $csv | Export-Csv  $file -NoTypeInformation;
    

    So in other words, just move output to the file out of the foreach loop and update only at the end, and it will take you a blink of an eye to complete the update
    regards
    fra

You must be logged in to reply to this topic.