Powershell – Storing Directory Folder Names Into Array Powershell

arraysdirectorypowershell

I am trying to write a script that will get the names of all the folders in a specific directory and then return each as an entry in an array. From here I was going to use each array element to run a larger loop that uses each element as a parameter for a later function call. All of this is through powershell.

At the moment I have this code:

function Get-Directorys
{
    $path = gci \\QNAP\wpbackup\

    foreach ($item.name in $path)
    {
        $a = $item.name
    }
}   

The $path line is correct and gets me all of the directories, however the foreach loop is the problem where it actually stores the individual chars of the first directory instead of each directories full name to each element.

Best Answer

Here's another option using a pipeline:

$arr = Get-ChildItem \\QNAP\wpbackup | 
       Where-Object {$_.PSIsContainer} | 
       Foreach-Object {$_.Name}