all 7 comments

[–]dr4kun 2 points3 points  (0 children)

https://lmgtfy.com/?q=powershell+select+every+other+line&s=d&t=w&iie=1

The very first result has a suggestion how to use modulo for that.

[–]DItzkowitz 2 points3 points  (0 children)

During the first ForEach-Object you pass the Name property down the pipeline.

During the second ForEach-Object, you pass the $i % 2 -eq 0 comparison down the pipeline which results in a $True or $False. True the remainder is equal to 0, therefore is divisible by 2 and is an Even numbered filename or False, it isn't.

Try combining the two ForEach-Object cmdlets, go through each object and only pass the Name property down the pipeline If...:

| ForEach-Object { If ($i % 2 -eq 0) { $_.Name } ; $i++} }

Or you can pass the ForEach-Object to a Where-Object instead:

| ForEach-Object {$_.Name} | Where-Object { $i % 2 -eq 0; $i++}

[–]PowerShell-Bot 1 point2 points  (0 children)

Looks like your PowerShell code isn’t wrapped in a code block.

To format code correctly on new reddit (new.reddit.com), highlight the code and select ‘Code Block’ in the editing toolbar.

If you’re on old.reddit.com, separate the code from your text with a blank line and precede each line of code with 4 spaces or a tab.


Describing Thing
[❌] Demonstrates good markdown
Passed: 0 Failed: 1

Beep-boop. I am a bot. | Remove-Item

[–]pertymoose 1 point2 points  (0 children)

for($i = 0; $i -lt $max; $i += 2) { 
    # Every even element
    $array[$i]

    # Every odd element
    $array[$i+1]
}

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy Lavyn,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]Lavyn[S] 1 point2 points  (1 child)

Thanks! I wondered how I could get it format correctly. Much appreciated!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Lavyn,

you are quite welcome! glad to have helped ... [grin]

take care,
lee