Need help to search csv file for values like 100 GB (107,374,182,400 bytes) or 50 GB (53,687,091,200 bytes) RRS feed

  • Question

  • Hello Folks,

    I am trying to read csv file using PowerShell which has values like 100 GB (107,374,182,400 bytes) or 50 GB (53,687,091,200 bytes) and column name like ProhibitSendReceiveQuota. I tried below code just for a test but it is not working and no error as well

    foreach ($line in (Import-Csv "C:\Temp\data.csv"))
      # Check if the 'ProhibitSendReceiveQuota' value in the line is 50 GB

      if ($line.ProhibitSendReceiveQuota -eq "50 GB")
       #If ProhibitSendReceiveQuota is 50 GB then store output in csv file

       $line.Alias | Out-File -FilePath "C:\Temp\Test_50GB.csv" -Append

    I even tried using pattern like below

    $csv = Import-Csv "C:\Temp\data.csv"

    $pattern = "^.*?ProhibitSendReceiveQuota Size: (?50\d+(?:\.\d+){0,1}.*)$"

    $csv | Select-String -Pattern $pattern

    And I got error -

    Select-String : The string ^.*?ProhibitSendReceiveQuota Size: (?50\d+(?:\.\d+){0,1}.*)$ is not a valid regular expression: parsing
    "^.*?ProhibitSendReceiveQuota Size: (?50\d+(?:\.\d+){0,1}.*)$" - Unrecognized grouping construct.
    At line:1 char:17
    + $finds = $csv | Select-String -Pattern $pattern
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Select-String], ArgumentException
        + FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand

    How can I achieve this? Any help in this regards is greatly appreciated.

    Thank you



    • Edited by abhvp Thursday, August 9, 2018 5:47 AM To make it clear
    Thursday, August 9, 2018 5:46 AM

All replies

  • Hi Abhay,

    You're regular expression is invalid, hence the error.

    In your first example, if the value in the csv is '53,687,091,200 bytes' and not equal to the string '50 GB', it will not prompt with any results. The string value has to be equal to the one provided in the csv. You can also use regular expressions in the first example using -match.

    Best regards,


    Thursday, August 9, 2018 6:13 AM
  • Don't use RegEx.  Convert the value to a number and test for numeric range.  No other way will work reliably.

    [long]$line.ProhibitSendReceiveQuota -ge 50Gb


    Thursday, August 9, 2018 6:47 AM