When writing a Bash script you can use brace expansion to quickly generate lists:
I have a way to do it using int's tostring method. The '000' at the end is a special format code. It always pads to the right number of zeros. You can also use wildcards with method names like t*g if you really want to be terse and mysterious.
1..10 | % tostring computer000
computer001
computer002
computer003
computer004
computer005
computer006
computer007
computer008
computer009
computer010
1..10 | % t*g 192\.168\.1\.0
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
'x' is also a format code for hex printing.
10..15 | % tostring x
a
b
c
d
e
f
There's always -replace, which also works on arrays. '^' is regex for 'beginning of line'. Use '$' instead for 'end of line'.
(echo test dev prod) -replace '^','server-'
server-test
server-dev
server-prod
Hah, I never tried this before.
(echo test dev prod) -replace (echo ^ server-)
server-test
server-dev
server-prod
Maybe they could do that brace expansion in powershell 8...
PS C:\> "test","dev","prod" | % { "server-$_" }
server-test
server-dev
server-prod
PS C:\> 1..5 | % { "server{0:D2}" -f $_ }
server01
server02
server03
server04
server05
PS C:\> 1..5 | % { "192.168.0.$_" }
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
Note that %
is an alias for the ForEach-Object
cmdlet.
I'm hoping to be proven wrong here, but I don't believe there is a way to do it exactly like with bash, or with as few keystrokes.
You can iterate over the list by piping it through a foreach-object
to achieve the same result though.
1..5 | foreach-object { "test" + $_ }
Or using the shorthand:
1..5 | %{"test$_"}
In both cases (%
is an alias for foreach-object
), the output is:
test1
test2
test3
test4
test5
Note: if you're building this into a script for publishing/distribution/reuse, use the more verbose foreach-object
, not the shorthand %
- for readability.