R
R
Roman Kharchenko2020-06-02 03:48:13
PowerShell
Roman Kharchenko, 2020-06-02 03:48:13

How to sort an array of paths in PowerShell?

Good afternoon.

There is a PS script.
I get a list of all directories for the specified directory $path

$itemFolders = Get-ChildItem $Path

    $AllFolders = $itemFolders | Get-ChildItem -Recurse | Where-Object {$_.PSIsContainer} | Select -Property FullName


The output of the variable is something like this:

FullName
--------
D:\SHARES\POSTBOX\IT\1
D:\SHARES\POSTBOX\IT\2
D:\SHARES\POSTBOX\IT\3
D:\SHARES\POSTBOX\IT\2\ 22
D:\SHARES\POSTBOX\IT\3\33
D:\SHARES\POSTBOX\IT\3\33\333


How to sort this list\array to "read directories" from the end:

FullName
--------
D:\SHARES\POSTBOX\IT\3\33\333
D:\SHARES\POSTBOX\IT\3\33
D:\SHARES\POSTBOX\IT\3
D:\SHARES\ POSTBOX\IT\2\22
D:\SHARES\POSTBOX\IT\2
D:\SHARES\POSTBOX\IT\1

or

FullName
--------
D:\SHARES\POSTBOX\IT\3\33\333
D:\SHARES\POSTBOX\IT\3\33
D:\SHARES\POSTBOX\IT\2\22
D:\ SHARES\POSTBOX\IT\3
D:\SHARES\POSTBOX\IT\2
D:\SHARES\POSTBOX\IT\1


It is necessary to bypass directories from below.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
azarij, 2020-06-02
@Sarymian

and so?

$а = 1,2,3,4,5
[array]::reverse($a)
$a

M
MaxKozlov, 2020-06-02
@MaxKozlov

Sort-Object -Descending
btw,

Get-ChildItem -Recurse | Where-Object {$_.PSIsContainer}
-->Get-ChildItem -Directory -Recurse

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question