How to check if a property is null/empty in a CSV

Welcome Forums General PowerShell Q&A How to check if a property is null/empty in a CSV

Viewing 10 reply threads
  • Author
    Posts
    • #221706
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Hi,

       

      I have a CSV with several properties like PersonalArea;Employeenumber;…

      I want to count the lines in this CSV but only these where the property “Employeenumber” is not null and the PersonalArea is equal  to “myLocation”.

      Currently, I got the following code:

       

      Here is an CSV example:

      PersonalArea;Employeenumber

      myLocation;123456

      myLocation;234567

      otherLocation;345678

      otherLocation;

       

      Currently, I get the following output:

      $a.count –> 0

      $CSV.count –> 4

       

      Can someone tell me why I getting a wrong output on $a.count?

      I appreciate your help.

      • This topic was modified 9 months, 1 week ago by tim--9706.
      • This topic was modified 9 months, 1 week ago by tim--9706.
      • This topic was modified 9 months, 1 week ago by tim--9706.
    • #221718
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      If I got it right you should check for an empty string instead of $null because Import-Csv only returns strings not objects.

    • #221724
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      I already tested it out with an empty string..same result

      OR

    • #221730
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      And why not just

    • #221739
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      I didn’t know this works. But it doesn’t work either.

    • #221742
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      It does work for me with my sample data. 😉 Could you please post a few sanitized but still representative rows of your CSV file? Please format it as code.

    • #221745
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      I’m using the same CSV data for testing as I mentioned above. Can you post your code?

      • This reply was modified 9 months, 1 week ago by tim--9706.
    • #221754
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      That’s the actual code I’m using to check if it works.
      The output is this:

    • #221760
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Now it works. First I have added the brackets + .count

      Then I removed these changes and tested it again with my code…

      The output is correct in both test cases.

      Thank you. Could you explain why you are using $_.Employeenumber instead of $_.Employeenumber -ne ‘ ‘ ? Is it just shorter or is there another reason?

      I think the problem was the piping of Measure-Object

      • This reply was modified 9 months, 1 week ago by tim--9706.
    • #221796
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      I think the problem was the piping of Measure-Object

      Ooops … I missed that completely until you just mentioned it now. Sorry. 😉

      Could you explain why you are using $_.Employeenumber instead of $_.Employeenumber -ne ‘ ‘ ? Is it just shorter or is there another reason?

      Mostly I’m really bad with explaining of what’s going on under the hood. Personally I like to write the code as short as possible but still as descriptive and detailed as possible without using aliasses or abbreviations.

      BTW: Sometimes it’s easier to ask a quesiotn in your native language. There are German Powershell-related forums as well like MCSEBoard.de or the German Microsoft Technet Powershell forum.

      Tschüß 😉

    • #221802
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      BTW: Sometimes it’s easier to ask a quesiotn in your native language. There are German Powershell-related forums as well like MCSEBoard.de or the German Microsoft Technet Powershell forum.

      Danke für den Tipp 🙂 Vielen Dank für die Hilfe.

      Tschüss.

Viewing 10 reply threads
  • The topic ‘How to check if a property is null/empty in a CSV’ is closed to new replies.