Looks like you don't want an actual list, but a string with a listing of variables. A multiline string with the names and values of your variables could be defined like this:
Copy$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
Another, more dynamic, option would be something like this:
Copy$list = 'a', 'b', 'c' | ForEach-Object {
"${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
where you specify a list with the variable names, output strings containing each variable name and the corresponding value, and merge those into a single string at the end.
Answer from Ansgar Wiechers on Stack OverflowVideos
Looks like you don't want an actual list, but a string with a listing of variables. A multiline string with the names and values of your variables could be defined like this:
Copy$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
Another, more dynamic, option would be something like this:
Copy$list = 'a', 'b', 'c' | ForEach-Object {
"${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
where you specify a list with the variable names, output strings containing each variable name and the corresponding value, and merge those into a single string at the end.
Powershell has 'arrays':
"An array is a data structure that is designed to store a collection of items. The items can be the same type or different types."
In your case, if you have variables already, you can include them in an array:
Copy$myarray = $a, $b, $c
Write-Host $myarray
or
Copy$myarray = @($a, $b, $c)
Write-Host $myarray
or you can create an associative array for enter your key-value elements:
Copy$otherarray = @{} # create en empty array
$otherarray['a'] = 'string1' # add key-value pairs
$otherarray['b'] = 'string2'
$otherarray['c'] = 'string3'
Write-Host $otherarray.c #print in console example
Edit: by your example I recommended arrays because I think it's a simple use case, but if you will modify it frecuently, better use System.Collection.ArrayList:
Copy$mylist = New-Object System.Collections.ArrayList
$mylist.add($a)
$mylist.add($b)
$mylist.add($c)
$Write-Host $mylist
The direct equivalent of that command in PowerShell would be dir -n > dirlist.txt.
The long (canonical) form of the command is Get-ChildItem -Name > dirlist.txt.
If you want to include subdirectories, that's dir -n -r > dirlist.txt and Get-ChildItem -Name -Recurse > dirlist.txt respectively.
dir in PowerShell is simply another name for Get-ChildItem, but unfortunately has different option names (-n vs /b) compared to cmd's dir command of the same name.
Do you see that menu option, "Open Power_S_hell window here"? Well, you can select that, and get one of those blue-windowed PowerShell windows.
Then, instead of running:
dir /b optional-target > output.txt
You can run this:
cmd
dir /b optional-target > output.txt
exit
Yes, the process is longer. That's the downside. The upsides are that it's an easy workaround, and this process lets you do the same things, in the same way, that you did with the traditional DOS-like command prompt. (Also, having PowreShell run CMD is a technique that ought to work on basically any Windows 10 computer, not just the ones where you've taken the time to re-add a desirable menu option to a context menu.)
When you run "cmd", you'll notice the command prompt will stop saying "PS " at the start of your prompt. That is how you can tell that the computer is ready to accept the traditional-style syntax. Since your command prompt will technically be using CMD, you won't have Powershell trying to insist on you using new parameters/options, and you won't need to worry about PowerShell doing screwy stuff with unescaped commas and quotation marks (until you run the first exit command, which leaves CMD and goes back to PowerShell).