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 6899605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:26:33+00:00 2026-05-27T07:26:33+00:00

I have a simple bit of code as follows: foreach ($someFile in Get-ChildItem) {

  • 0

I have a simple bit of code as follows:

foreach ($someFile in Get-ChildItem) {
    if (($someFile.Name.Substring(0,1) -eq "R") -or ($someFile.Name.Substring(0,1) -eq "S") {
        $foreach.MoveNext() | Out-Null
        $someFile.Name
    }
}

The idea of this simple example was to output all file names except for those that begin with the letter “R” or “S”.

When I run this in a directory with one file that begins with “R” and one that begins with “S” along with some other files it skips over the “S” file as intended, but the “R” file name still gets displayed.

I know that there are other ways that I can accomplish this, but this was an attempt to make sure that I was using the foreach correctly before using it in a more complicated bit of code.

Can anyone see something wrong with the code above? It seems like it should work to me. I’ve also confirmed that $someFile.Name.Substring(0,1) -eq "R" returns True for the “R” file as expected.

Thanks!

  • 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-27T07:26:34+00:00Added an answer on May 27, 2026 at 7:26 am

    Currently you are returning $someFile.Name, so that will give S file when R occurs and in the next iteration, it would go to T ( or whatever is next ) and not S. Logic also breaks down when there are multiple R files etc.

    If you just want to skip the R and S files, probably you want to just use continue:

    foreach ($someFile in Get-ChildItem)
    {
        if (($someFile.Name.Substring(0,1) -eq "R") -or ($someFile.Name.Substring(0,1) -eq "S"))
        {
            continue
    
        }
        $foreach.Current.Name  # $somefile.Name
    }
    

    Also, you can use String.StartsWith rather than the SubString. Or use -like and combine it into a single check.

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

Sidebar

Related Questions

I have a very simple bit of code that is supposed to capture the
I have a really simple bit of code that I'm trying to in Ms
I have a bit of sample code that is throwing this warning: main.c: In
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I have a simple bit of jQuery which displays the row below the current
I have a very simple bit of jQuery to retrieve my latest Tweet $.getJSON(http://twitter.com/statuses/user_timeline/username.json?count=1,
simple bit of jquery. I have a div with clss linklist, inside it has
I am using ubuntu 12.04 on 64 bit machine, I have this simple C
I have a pretty simple ASP.NET Web Form that looks a bit like the
I am a bit confused. I have created a simple form with a one

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.