I have a folder named D:\MyDir, and this is its content:

PS D:\MyDir> gci

    Directory: D:\MyDir

Mode                LastWriteTime         Length Name
----                -------------         ------ ---- d-----       11/22/2021   8:10 PM                MyFolder-2021-11-22

I'm storing the folder's name ordered by creatin on $Dirlist:

$Dirlist = gci $LBDestination | ? { $_.PSIsContainer } | sort CreationTime | select -Property Name | foreach { $_.Name }

The problem I am having is that the system is storing each letter of my listing as one value in my array

PS D:\MyDir> $Dirlist[0..3]

When I have more than one folder my script works fine.

New contributor
Wayne Hamilton is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

It's not a good practice to use format-list for handling data. format-list is for visualizing output.

Instead, you should tell your variable that it definetly is an array, even if it has only one value. You can do that by casting it to a [string[]] or by wrapping your entire command into @() which makes it an array even before it's stored in the variable.

See the difference here:

Your way works if there is more than one return, since then it automatically is an array. However it does not work if there is only a single return, since PowerShell thinks you want your Variable to be a string, because you don't specifically tell it that you want it to be an array:

PS C:\Install\mydir> $Dirlist = gci . | ? { $_.PSIsContainer } | sort CreationTime | select -ExpandProperty Name
PS C:\Install\mydir> $Dirlist[0..3]

You can cast it to an array and it will work:

PS C:\Install\mydir> [string[]]$Dirlist = gci . | ? { $_.PSIsContainer } | sort CreationTime | select -ExpandProperty Name
PS C:\Install\mydir> $Dirlist[0..3]

Or wrap the entire command in @():

PS C:\Install\mydir> $Dirlist = @(gci . | ? { $_.PSIsContainer } | sort CreationTime | select -ExpandProperty Name)
PS C:\Install\mydir> $Dirlist[0..3]

btw. instead of

  • | select -Property Name | foreach { $_.Name }

just use

  • | select -ExpandProperty Name
  • Why is not a good practice to use format-list? What is the difference between using "| select -ExpandProperty Name"? Nov 24 at 10:17
  • 2
    @WayneHamilton Format-List works in this case because you have single strings as an array. however if you have multiple properties etc. it won't work anymore as you expect it. as I said it's for visualizing data, just for you to look at it, not for handling and manipulating data. it would be a bad habit if you think now "oh i can just fl everything". So don't use it at all for such cases, it's simply not correct. the select i wrote is just more elegant because it expands the property directly and you don't need to iterate it with foreach. it does exactly the same but is more elegant.
    – SimonS
    Nov 24 at 10:33
  • Thank you for the explanation @SimonS. Nov 24 at 18:46

I just added a format-list (ft is the short version) in the end and it worked for me:

$Dirlist= gci $LBDestination | ? { $_.PSIsContainer } | sort CreationTime | select -Property Name | foreach { $_.Name } | fl

Your Answer

Wayne Hamilton is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.