3

I am attempting to use a single element of an array, when creating a string message, but it is using both elements, concatenated by a space, whether I use $myArray[0] or $myArray[1]...

# Multiple distinct log folders found
Write-Host "Multiple script directories were found in the command list."
Write-Host "Which folder would you like to use for logging?"

for ($i = 0; $i -lt $uniqueLogFolders.Count; $i++) {
    $marker = if ($i -eq 0) { " (default)" } else { "" }
    Write-Host "$($i+1). $uniqueLogFolders[$i]$marker"
}

$choice = Read-Host "Enter the number of the folder to use [default = 1]"

When debugging in Visual Studio, I can clearly see that the 2 element array is, as I expect it to be, containing 2 unique folders...

Screenshot of Visual Studio debugger, showing the $uniqueLogFolders array

But the resulting output is as follows...

Multiple script directories were found in the command list. Which folder would you like to use for logging?

  1. C:\Users\Todd\source\repos\MyProject\.scripts\logs C:\Users\Todd\source\repos\MyProject\logs[0] (default)
  2. C:\Users\Todd\source\repos\MyProject\.scripts\logs C:\Users\Todd\source\repos\MyProject\logs[1]

Enter the number of the folder to use [default = 1]:

I've spent 2 days, racking my brain over this and I'm completely stumped! Someone please save me!

Powershell Version: 5.1.26100.7019

2
  • you used $(...) in $($i+1) to evaluate that sub-expression, you're just missing it again in $uniqueLogFolders[$i]. powershell isn't able to tell you want to actually evaluate that indexing expression instead of using literal brackets in your string without it ;) Commented 10 hours ago
  • @SantiagoSquarzon Oh my... What a stupid mistake! 🤦🏾‍♂️ Write that up as an answer and I'll mark it as correct. Commented 10 hours ago

2 Answers 2

1

Your issue, as explained in comments, is due to the lack of the subexpression operator $( ) to evaluate the indexing expression $uniqueLogFolders[$i] in your string. Without it, PowerShell is converting the $uniqueLogFolders array to a string; the array elements get joined by $OFS (a space by default) when this happens and the brackets [..] are taken literally.

for ($i = 0; $i -lt $uniqueLogFolders.Count; $i++) {
    $marker = if ($i -eq 0) { " (default)" } else { "" }
    Write-Host "$($i + 1). $($uniqueLogFolders[$i])$marker"
}

You can, alternatively use PowerShell's version of string.Format, the -f operator:

for ($i = 0; $i -lt $uniqueLogFolders.Count; $i++) {
    $marker = if ($i -eq 0) { " (default)" } else { "" }
    "{0}. {1}$marker" -f ($i + 1), $uniqueLogFolders[$i] | Write-Host
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out my stupid mistake @SantiagoSquarzon! 🤦🏼‍♂️ With that level of snafu, I'm sure it's obvious I'm just learning Powershell. So thanks for the -f suggestion as well!
0

Not an answer to your (already answered) question, but a different approach to your menu.
Instead of handcrafting a menu, an easy way to provide a selection in PowerShell is to use the underrated Out-GridView.
By default, it will just show whatever it is told to show, and the script will continue.
But with the -PassThru or the -OutputMode parameter, it will wait for the user to select something, and return the row(s) selected.
So your menu part of the script could look like this:

# Multiple distinct log folders found
Write-Host "Multiple script directories were found in the command list."
Write-Host "Please select one in the form that just popped up."

$chosenLogFolder = $uniqueLogFolders | Out-GridView -Title "$($MyInvocation.MyCommand.Name): found multiple script directories; select one to use for logging:" -OutputMode Single
If (-not $chosenLogFolder) {
    Write-Warning 'No folder selected, aborting.'
    Return
}

2 Comments

Thanks for that idea. It's a neat little way to prompt for input. However, it only worked when I ran Powershell and changed to the script folder to execute it. I normally call this script from within Visual Studio's Powershell Terminal window. (Right-Click Project, Open in Terminal) When called from there, the script just paused and I had to Ctrl+C to break. Evidently the Out-GridView doesn't work from that environment. 🤷🏾‍♂️
Also... I had my "choose from list" configured, to automatically use the first option if the user pressed Enter, without selecting an entry. That one was labeled as (default). Also, can you specify a size for the Out-GridView? It took up my entire screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.