0

I've looked at various sites that suggest that Sort-Object will return an array. This is not what I am seeing as when I do the below, the final output caused by the Sort-Object is just a single string. How can I sort the array into another array instead of to a string?

$myprogs = Get-ChildItem D:\MyPath\MyPrograms
$myprognames = foreach ($i in $myprogs) { $i.FullName -replace "D:\\MyPath\\MyPrograms\\", "" }
# At this point, $myprognames is a normal array
$myprognames = $myprognames | Sort-Object -Unique
$myprognames
# At this point, $myprognames is a single string with all elements stuck together, and if I select an element, like $myprognames[17], I will just get a single character as it's a string.
2
  • When I do the $i.FullName, that's just a string, so I'm creating an array of strings, right? Hence, I just want to sort the strings out into an ordered set of strings and save them as an array. The strings themselves are the set of objects that I want to sort alphabetically. i.e. I'm basically throwing away all of the properties that the Get-ChildItem returns and just want the .FullName strings. I don't see what you mean about not saving my changes, sorry? Commented Nov 2, 2022 at 7:32
  • 1
    Updated my answer. Take a look. Commented Nov 2, 2022 at 7:43

1 Answer 1

1

If I assume right you want something similar to this:

$myprogs = 
    Get-ChildItem 'D:\MyPath\MyPrograms' |
        Select-Object -Property *,
            @{Name = 'ShortenedPath'; Expression = {$_.FullName -replace ([regex]::escape('D:\MyPath\MyPrograms'))}}
$MyProgNames = $myprogs | 
    Sort-Object -Property ShortenedPath -Unique

Of course you should list all properties you're actually after instead of using -Property *

According to your comment you're actually looking for the names of the folders sorted, right? If that's the case you're overcomplicating this a lot. ;-)

$myprogs = 
    Get-ChildItem 'D:\MyPath\MyPrograms' |
        Select-Object -Property Name |
           Sort-Object -Property Name -Unique
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I had a specific reason for doing this, but didn't want to complicate the question. I need only the names in this way, because I am collecting target paths of shortcuts from a different system that have different relative paths to my local folders and that include the target executable in the names of that side, so I first want to strip off the exectuable, then strip off the common prefix part of the path (D:\MyPath\MyPrograms) and then sort those simple strings. You are right that the Name property correctly fits the question as I asked it. 👍
Do you know why my array of strings in the OP is getting stuck together into a single string instead of sorting into another array of strings when I use Sort-Object on it (my preferences is that the the array is opened up, sorted and then a new sorted array is created)?
Actually no. And I cannot reproduce this behaviour. I get the output I'd expect from the code.

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.