0

First I have tried to locate other questions and these all seem related to replacement rather than expansion.

So I must first create clarity on what I mean by "expansion".

> $TEST="Foo Bar"
> echo foo bar
foo
bar
> echo $TEST
Foo Bar

When passing two parameters, powershell's implementation of echo will print each parameter on its own line.

If I echo a variable with two parameters it is passed as a single parameter. I would like the variable to be expanded into the multiple arguments which it contains, getting the behavior from the first instance.

I have looked at:

  • Single quotes
  • Parentheses
  • Curly braces in different forms

Is this possible in powershell?

Context Update:

Unfortunately I'm not the one setting the variable, this comes from gitlab environment variables.

he_the_great
  • 6,554
  • 2
  • 30
  • 28
  • `echo (-split $TEST)` maybe? Another way: `echo $TEST.split()` – zett42 Mar 12 '21 at 20:35
  • 1
    "If I echo a variable with two parameters" - the variable has no parameters - it just contains the 1 string that you created with `"Foo Bar"`. Try `$TEST = "Foo", "Bar"` in comparison – Mathias R. Jessen Mar 12 '21 at 20:40
  • ```echo foo bar``` is an alias for ```Write-Output @("foo", "bar")``` - i.e. write out an array containing two strings, whereas ```echo $TEST``` is basically ```Write-Output "Foo Bar"``` - i.e. output a single string. If you do what @Mathias R. Jessen says you're setting the variable ```$TEST``` to be an array of 2 strings so it'll output them on separate lines. – mclayton Mar 12 '21 at 20:43

2 Answers2

3

It sounds like you want the @ splat operator:

$test = "foo","bar"
echo @test

This will have the exact same effect as echo foo bar

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
1

How to pass a list of arguments into a powershell commandlet?

Let's say you need to pass a bunch of CSV header fields into -Header argument, that is part of the ConvertFrom-Csv commandlet in order to convert an array into a CSV.

# The test array
$pac = @("app1", "app2")

# The needed command:
$pac | ConvertFrom-Csv -Header id, gui, src, sco, name, loc
# This works, but is too verbose, and can't be modified.

# So you Try-1:
$hdr = 'id,gui,src,sco,name,loc'
$pac | ConvertFrom-Csv -Header $hdr
# Not working!

Q: What is going on?
A-1: It's complicated, but solution is easy, once you find it.
A-2: There are 3 main solutions to skin this cat.

Solution-1
Write the header variable as a string, and use the -split operator inline.
Caveats: Watch you spaces! And harder to read.
Advantage: Simple.

$hdr = 'id,gui,src,sco,name,loc'
$pac | ConvertFrom-Csv -Header ($hdr -split ',')

Solution-2
Write the header variable as a quoted list.†
Caveats: Still long and verbose.
Advantage: Easy to read.
I was never able to use the splat @ operator here.

$hdr = 'id', 'gui', 'src', 'sco', 'name', 'loc'
$pac | ConvertFrom-Csv -Header $hdr

Solution-3
Write the header as a tricky [just my way to name it] argument function that only expands its arguments.
Caveats: Is tricky at first sight.
Advantage: No quotes and easy to read, and can be reused.

function ql { $args }
$hdr = (ql id, gui, src, sco, name, loc)
$pac | ConvertFrom-Csv -Header $hdr
not2qubit
  • 14,531
  • 8
  • 95
  • 135