20

Why does $dlls.Count return a single element? I try to declare my array of strings as such:

$basePath = Split-Path $MyInvocation.MyCommand.Path

$dlls = @(
    $basePath + "\bin\debug\dll1.dll",
    $basePath + "\bin\debug\dll2.dll",
    $basePath + "\bin\debug\dll3.dll"
)
Bruno
  • 4,685
  • 7
  • 54
  • 105
  • Possible duplicate of [PowerShell array initialization](http://stackoverflow.com/questions/226596/powershell-array-initialization) – Kory Gill Jun 11 '16 at 00:03

4 Answers4

28

You should use something like:

$dlls = @(
    ($basePath + "\bin\debug\dll1.dll"),
    ($basePath + "\bin\debug\dll2.dll"),
    ($basePath + "\bin\debug\dll3.dll")
)

or

$dlls = @(
    $($basePath + "\bin\debug\dll1.dll"),
    $($basePath + "\bin\debug\dll2.dll"),
    $($basePath + "\bin\debug\dll3.dll")
)

As your answer shows, the semicolons also work because that marks the end of a statement...that would be evaluated, similar to using parenthesis.

Alternatively, use another pattern like:

$dlls = @()
$dlls += "...."

But then you might want to use ArrayList and gain performance benefits...

See PowerShell array initialization

Community
  • 1
  • 1
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • `[string[]]$Script:developmentApps = "git", "vscode", "gpg4win", "postman", "grepwin"` in multiline ? – Kiquenet Jan 30 '23 at 14:20
9

You are combing a Path, thus use the Join-Path cmdlet:

$dlls = @(
    Join-Path $basePath '\bin\debug\dll1.dll'
    Join-Path $basePath '\bin\debug\dll2.dll'
    Join-Path $basePath '\bin\debug\dll3.dll'
)

You don't need to use any comma, semicolon nor parenthesis. Also see this answer.

Community
  • 1
  • 1
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • 5
    ah thank you. why are commas optional? why does powershell not seem to have any consistent syntax? this is just more confusing lol – Bruno Jun 11 '16 at 15:46
  • 2
    @ibiza I wouldn't call them optional, since that would imply having them would work as well, which is not the case. So it is mandatory not to use commas (unless you wrap the entries in parentheses as demonstrated in other answers). – Ohad Schneider Dec 04 '19 at 13:59
4

I found it, I must use semi-colons instead of commas...anyone can explain why?

It's clearly commas according to pretty much any source (such as this one)

$basePath = Split-Path $MyInvocation.MyCommand.Path

$dlls = @(
    $basePath + "\bin\debug\dll1.dll";
    $basePath + "\bin\debug\dll2.dll";
    $basePath + "\bin\debug\dll3.dll";
)
Bruno
  • 4,685
  • 7
  • 54
  • 105
  • 2
    All of this works: using comma, semicolon, or simply new lines. (tested in PowerShell 6.2) – MovGP0 Feb 13 '20 at 11:59
0

Sorry for re-opening, but to me seems like it's missing the simplest and more natural way of declaring arrays in powershell:

$basePath = Split-Path $MyInvocation.MyCommand.Path
$dllDir = "$basePath\bin\debug"
$dlls = `
   "$dllDir\dll1.dll",
   "$dllDir\dll2.dll",
   "$dllDIr\dll3.dll"

The backtick after the declaration of the dlls variable is just an escape for the new line character, just for readability.