Add all dirPaths open in windows explorer to an array

I’m coming as a newbie to PowerShell from python.
I found the following snippet that, among other things, in effect, lists all open dirs in windows explorer:

(New-Object -ComObject ‘Shell.Application’).Windows() | ForEach-Object {
$localPath = $_.Document.Folder.Self.Path
“C:\WINDOWS\explorer.exe /e, "$localPath”"
}

I would like to know how to create an array variable (e.g., $myArray) and use a foreach loop to populate the array with the dirPaths.

Foreach (placeholder variable IN collection)
{ What I want to do to the placeholder variable}

Any tips would be much appreciated.

Marc B.,
Welcome to the forum. :wave:t4:

PowerShell is made for administrators - not programmers. :wink: So a lot of explicit techniques you need in other programming or scripting languages are done implicitly by PowerShell for you without the need for you to even think about …

I’d recommend to do a step back and learn the fundamentals of PowerShell first. There you will learn that PowerShell has its built in help system where you find all needed basic information.
For example - you can run

Get-Help about_foreach

to get help for the foreach statement. You could add -ShowWindow to get it in a separate window for better readabilty - you can zoom it or search in it.

Get-Help about_foreach -ShowWindow

And for a lot of cmdlets you can add the paramter -Online to open the online version of the help where you can jump to other linked help topics if you need

Get-Help ForEach-Object -Online

You should ALWAYS read the help for the cmdlets you’re about to use completely including the examples to learn how to use them. :wink:

To get an array of items you enumerate in PowerShell you can simply assign the output of the enumeration to a variable. PowerShell will make it an array automatically if there are more than one elements:

$FolderList =
(New-Object -ComObject 'Shell.Application').Windows() | 
ForEach-Object {
    $_.Document.Folder.Self.Path
}

… you can cast the output to an array even if there’s only one element in it with the array sub-expression operator @()

$FolderList = @(
(New-Object -ComObject 'Shell.Application').Windows() | 
    ForEach-Object {
        $_.Document.Folder.Self.Path
    }
)

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

1 Like

Thank you very much.
I’ll be studying the lesson you gave me for at least a week, trying out different things. I appreciate it very much.

May I ask you to tell me how to write the result to a .cmd file in the following format, for each FolderPath?
"C:\ProgramData\Anaconda3\python.exe" "e:\pyUtils\DoQqch.py" "FolderPath"

Is there a free web tutorial that you could recommend to learn the fundamentals of PowerShell?

Thank you again. Much appreciated. Marc

Why would you like to use an ancient club while you have a fully equiped and easy to use modern tool box? :man_shrugging:t4: :wink: :wink: :stuck_out_tongue_winking_eye:

Did you read the help for about_foreach and Foreach-Object?

You may read as well the help topic for

or the call operator …

Please read the help completely including the examples to learn how to use them:

There are several good tutorials out there. One of the most famous ones is this:

Don’t worry that it’s about the version 3.0. The basics are still the same. :wink:

1 Like