none
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 { 
    
     $xml.WriteStartElement('item');
    
     $xml.WriteAttributeString("ID",$_['ID']);
     $xml.WriteAttributeString("Title",$_['Title']);
     $xml.WriteAttributeString("Start",$_['Start']);
     $xml.WriteAttributeString("End",$_['End']);
     

    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?

    Thanks,

    Jakob

    Wednesday, July 16, 2014 7:41 AM

Answers

  • Hi Jakob,

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

    if ($_['End'] -eq $null)
    {
    	$xml.WriteStartElement('item');
    	
    	$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 { 


    Cheers,
    Fred


    There's no place like 127.0.0.1


    • 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