Adding a filter RRS feed

  • Question

  • Hi

    I'm a PowerShell newbie trying to filter the output of a ForEach-Object startement:

     $spItems | ForEach-Object { 

    I need to add a filter that ensures that only list items where the "End" column is empty, will be written.

    How do I do that?



    Wednesday, July 16, 2014 7:41 AM


  • Hi Jakob,

    you could wrap the content of the scriptblock your ForEach-Object consumes into an if statement:

    if ($_['End'] -eq $null)
    	$xml.WriteAttributeString("ID", $_['ID'])
    	$xml.WriteAttributeString("Title", $_['Title'])
    	$xml.WriteAttributeString("Start", $_['Start'])
    	$xml.WriteAttributeString("End", $_['End'])

    Another method would be to filter the items out before you pass them into ForEach-Object like this:

    $spItems | Where { $_['End'] -eq $null } | ForEach-Object { 


    There's no place like

    • Edited by FWN Wednesday, July 16, 2014 8:33 AM
    • Marked as answer by J Rohde Wednesday, July 16, 2014 8:37 AM
    Wednesday, July 16, 2014 8:30 AM