0

Is there some non ridiculous (foreach) way to reproduce the linux output with powershell?

echo A{0..9} A0 A1 A2 A3 A4 A5 A6 A7 A8 A9


Write-Host A(0..9) A 0 1 2 3 4 5 6 7 8 9

Thank you!

  • foreach($element in 1..9){ Write-Host -NoNewLine "A${element} "} Write-Host "" for me this compared to "echo A{0..9}" it's ridiculous. And thank you for your help. – Jacob Subirada Oct 03 '19 at 14:49

1 Answers1

1

I don't think this is ridiculous:

# If you want them on different lines
(0..9) | % {"A$_"} | Write-Host

# If you want them on the same line
(0..9) | % {"A$_ "} | Write-Host -NoNewline
Zach Alexander
  • 403
  • 4
  • 9
  • 2
    You can drop the `()` around the range expression :) – Mathias R. Jessen Oct 03 '19 at 14:48
  • If you want to [avoid Write-Host](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/RuleDocumentation/AvoidUsingWriteHost.md), you can use: `(0..9 | % {"A$_ "}) -join ''`. However, this still uses a `foreach`, since `%` is exactly equivalent (alias) to `ForEach-Object`, just much less readable. – boxdog Oct 03 '19 at 15:00