ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    powershell - delete row in csv file

    IT Discussion
    2
    2
    5.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      stess
      last edited by stess

      Hi again,

      I have a script that will loop through (ForEach-loop) the csv file. The script will test some criteria and if it is a match it will execute the command. Afterward, I want the script to delete that row as it is no longer valid. However, any other row that does not match the criteria should be left alone.This is what I have so far...

      $CsvPath = "E:\Docs\Documents\Scripts\OOO.csv"
      $CsvImport = Import-Csv $CsvPath
      $CsvDate = Get-Date -Format 'M/d/yyy'
      $CsvTime = Get-Date -Format 'hh:mm tt'
      foreach ($CsvItem in $CsvImport)
      {
      $CsvEmailFrom = $csvItem.emailfrom
      $CsvEmailTo = $csvitem.emailto
      $CsvStartDate = [datetime]$CsvItem.startdate
      $CsvStartDate = $CsvStartDate.ToShortDateString()
      $CsvStartTime = [datetime]$CsvItem.starttime
      $CsvStartTime = $CsvStartTime.ToString('hh:mm tt')
      $CsvEndDate = [datetime]$CsvItem.enddate
      $CsvEndDate = $CsvEndDate.ToString('hh:mm tt')
      $CsvEndTime = [datetime]$CsvItem.endtime
      $CsvEndTime = $CsvEndTime.ToString('hh:mm tt')
      $customTime = [datetime]"09:59AM"
      $customTime = $customTime.ToString('hh:mm tt')
      #Invalid start/enddate
      #($CsvStartDate -gt $CsvEndDate)

      if ($CsvDate -eq $CsvStartDate -and $CsvDate -le $CsvEndDate){
      if ($CsvTime -gt $CsvStartTime){
      $CsvStartDate.Remove($CsvStartDate)
      Export-Csv $CsvPath
      }}
      }

      The table is a simple table
      0_1516133691591_390798ab-eb03-460c-8795-1ad848ee2331-image.png

      Edit: I did found couple ways to do this, but the script they provided is a lot more complicate than I like it to be. I am trying to avoid my future self from going crazy trying to understand WTF my past self has wrote.

      1 Reply Last reply Reply Quote 0
      • matteo nunziatiM
        matteo nunziati
        last edited by

        did you looked at this?

        it uses the Remove method and seems quite straightforward to me. You just need to rework the for loop or simply add a counter to it to know which row has to be purged.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post