Question Details

No question body available.

Tags

arrays powershell string-interpolation powershell-5.1

Answers (2)

Accepted Answer Available
Accepted Answer
November 29, 2025 Score: 2 Rep: 66,174 Quality: High Completeness: 80%

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" }

Alternatively, you can 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 }
November 29, 2025 Score: 0 Rep: 326 Quality: Low Completeness: 80%

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 }