Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:57:09+00:00 2026-05-18T09:57:09+00:00

I often have the following situation in my PowerShell code: I have a function

  • 0

I often have the following situation in my PowerShell code: I have a function or property that returns a collection of objects, or $null. If you push the results into the pipeline, you also handle an element in the pipeline if $null is the only element.

Example:

$Project.Features | Foreach-Object { Write-Host "Feature name: $($_.Name)" }

If there are no features ($Project.Features returns $null), you will see a single line with “Feature name:”.

I see three ways to solve this:

if ($Project.Features -ne $null)
{
  $Project.Features | Foreach-Object { Write-Host "Feature name: $($_.Name)" }
}

or

$Project.Features | Where-Object {$_ -ne $null) | Foreach-Object { 
  Write-Host "Feature name: $($_.Name)" 
}

or

$Project.Features | Foreach-Object { 
  if ($_ -ne $null) {
    Write-Host "Feature name: $($_.Name)" }
  }
}

But actually I don’t like any of these approaches, but what do you see as the best approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:57:09+00:00Added an answer on May 18, 2026 at 9:57 am

    I don’t think anyone likes the fact that both “foreach ($a in $null) {}” and “$null | foreach-object{}” iterate once. Unfortunately there is no other way to do it than the ways you have demonstrated. You could be pithier:

    $null | ?{$_} | % { ... }
    

    the ?{$_} is shorthand for where-object {$_ -ne $null} as $null evaluated as a boolean expression will be treated as $false

    I have a filter defined in my profile like this:

    filter Skip-Null { $_|?{ $_ } }
    

    Usage:

    $null | skip-null | foreach { ... }
    

    A filter is the same as a function except the default block is process {} not end {}.

    UPDATE: As of PowerShell 3.0, $null is no longer iterable as a collection. Yay!

    -Oisin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll often have objects with properties that use the following pattern: private decimal? _blah;
I quite often have rows of code like the following: UPDATE my_table SET name
I have the following line of code called very often: var configValue = System.Configuration.ConfigurationManager.AppSettings[ConfigValueKey];
I often have a situation in my Java code when I need to set
I'm often confronted with the following situation: I have some data in a table
This is a situation that comes up often: In the View, you have a
I often have to debug Java code which was written so that there is
I often have to deal with XML documents that contain namespaced elements, but doesn't
I have found that I often have to implement some sort of a scheduler
When I have a controller, e.g. article I often have a action_view() that handles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.