Updating CSV cells

Welcome Forums General PowerShell Q&A Updating CSV cells

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #78865

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘Updating CSV cells’ is closed to new replies.