0

I am trying to filter the data contains in excel. but it showing error.

Import-Csv -Path "D:\Excel\Data\List.csv" -Header "EventID", "TimeCreated", "Computer", "TargetUserName", "SubjectUserName", "HOSTName", "Filename" | 
        Where-Object {[INT]$_.TargetUserName -eq 122388} | 
        Select-Object "EventID", "TimeCreated", "Computer", "TargetUserName", "SubjectUserName", "HOSTName", "Filename" | 
        Export-Csv "D:\Excel\test.csv" -NoTypeInformation

Excel Input data

Error while executing

2
  • By using the -header parameter you adding an extra header to the already existing header. Therefore the existing header becomes the first row where the top cell in the TargetUserName contains the string "TargetUserName" which can't be converted to an integer. Long story short: lose the -header parameter. Commented Jun 24, 2022 at 7:00
  • Additional note: the operand at the left hand side in a comparison operation is leading for type casting. In other words, by simply swapping the operands (122388 -eq $_.TargetUserName) you will do a number (integer) compare. Commented Jun 24, 2022 at 7:09

1 Answer 1

1

It's likely getting read in as a string instead of an integer. Do an equality test against a string instead.

Try replace Where-Object {[INT]$_.TargetUserName -eq 122388} with Where-Object {$_.TargetUserName -eq "122388"}

Sign up to request clarification or add additional context in comments.

2 Comments

How to do the equality test. I am new for the shell scripting. kindly help me @Toby Boulton
@Arunkumar check updated answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.