22

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.

5 Answers 5

46

Here's another option using a pipeline:

$arr = Get-ChildItem \\QNAP\wpbackup | 
       Where-Object {$_.PSIsContainer} | 
       Foreach-Object {$_.Name}
Sign up to request clarification or add additional context in comments.

5 Comments

This is a more complete solution. Should be marked as answer.
this is so useful, thanks: | Foreach-Object {$_.Name}
In my case, for some reason, this joins all dir names into a single string.
Here it also seems to be a string, though I need an array. Sorry, I'm new to Powershell...
That is unfortunately only working, if the directory \\QNAP\wpbackup more than one child. In case of exactly one, $arr will be an array of chars, not directoriesand a loop over will enumerate chars of that string. Good powershell ;)
10

$array = (dir *.txt).FullName

$array is now a list of paths for all text files in the directory.

Comments

7

For completeness, and readability:

This get all files in "somefolder" starting with 'F' to an array.

$FileNames = Get-ChildItem -Path '.\somefolder\' -Name 'F*' -File

This gets all directories of current directory:

$FileNames = Get-ChildItem -Path '.\' -Directory

Comments

6
# initialize the items variable with the
# contents of a directory

$items = Get-ChildItem -Path "c:\temp"

# enumerate the items array
foreach ($item in $items)
{
      # if the item is a directory, then process it.
      if ($item.Attributes -eq "Directory")
      {
            Write-Host $item.Name//displaying

            $array=$item.Name//storing in array

      }
}

Comments

3

I believe the problem is that your foreach loop variable is $item.name. What you want is a loop variable named $item, and you will access the name property on each one.

I.e.,

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

Also take note that I've left $item.name unassigned. In Powershell, if the result isn't stored in a variable, piped to another command, or otherwise captured, it is included in the function's return value.

2 Comments

Thank you very much. I changed to this and then assigned each return value to a var outside of the function. From here I can now use $a[xxx] to get the value that I want. Was unaware of your second comment about non stored values being returned that way.
Yeah. That's a little gotcha that bit me once or twice, especially since I didn't even realize I had unconsumed output at the time.

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.