Can´t sort Data

Our Accountstruture is not uniform. So I have to create my own view. Everythings works fine until i want to sort my seleted Data. I have no idea why it wont work. Every Idea is welcome.
Greetings, Franken


$Auswert = @(
				'CN= Bachhuber Hermann PN78945, DS, DA'
				'CN= Al Husainy Mohammed Wessam PN55710, DS, DA'
				'CN= Max Erwin Wundermann PN12345, DS, DA'
			)
foreach($INDS in $Auswert) 
{
	$DS = @( $INDS -split ",")
		
	#Abschneiden der ersten drei Zeichen des Datensatzes
	$DS = $DS[0].Substring(3)
	
	#Füllen Array mit Accountname
	$DS = @( $DS -split " ")
	
	
	#Z-Nummer / letzter Eintrag im Array
	#Write-Host $DS[-1]
		
    $j=0
	$KDS=@()
	for ($j=0; $j -le ($DS.Length-1); $j++)
	{
		if     ( $j -eq 0)            { $KDS = $DS[$j] }		  #Kein Leerzeichen am Anfang 	
		elseif ( $j -eq $DS.Length-1) { $KDS = $KDS + "`r`n" }    #CRLF am Ende
		else                          { $KDS = $KDS +" "+$DS[$j] }#Verbinden der Namen mir Leerzeichen
	}
	#Array mit AcountName füllen
	$User += $KDS
	#Array mit PN-Nummer füllen
	$PNummer += $DS[-1]
}
$User = $User | sort 
$PNummer1 = $PNummer | sort 
foreach($mem in $User) 
{
	$mem
}
 
 foreach($mem in $PNummer) 
{
	$mem
}

Can you format all your code as code and provide an example of the current output along with the desired output?

That the Way it is

Bachhuber Hermann
Al Husainy Mohammed Wessam
Max Erwin Wundermann

PN78945PN55710PN12345

This should be

Al Husainy Mohammed Wessam
Bachhuber Hermann
Max Erwin Wundermann

PN12345PN55710PN78945

The reason your sort is not working is because $User is one scalar string not an array of strings, so

$User | sort

does nothing. Maybe it’s the language barrier but I’m struggling to figure out your requirements. Do you just want all the names sorted?

I’d probably approach this problem with regex and custom objects. Something like this:

$Auswert = @(
'CN= Bachhuber Hermann PN78945, DS, DA'
'CN= Al Husainy Mohammed Wessam PN55710, DS, DA'
'CN= Max Erwin Wundermann PN12345, DS, DA'
)


$results = @($Auswert | ForEach-Object {
    $_ -match 'CN= (.+)(PN[0-9]+)' | Out-Null
    [pscustomobject]@{
        Name = $Matches[1]
        PN   = $Matches[2]
    }
})

$results | 
    Sort-Object -Property Name

Here is the output:

Name                        PN     
----                        --     
Al Husainy Mohammed Wessam  PN55710
Bachhuber Hermann           PN78945
Max Erwin Wundermann        PN12345

Here with English Comments

$Auswert = @(
‘CN= Bachhuber Hermann PN78945, DS, DA’
‘CN= Al Husainy Mohammed Wessam PN55710, DS, DA’
‘CN= Max Erwin Wundermann PN12345, DS, DA’
)

foreach($INDS in $Auswert)
{
$DS = @( $INDS -split “,”)

#Cut of the first three characters of the data set
$DS = $DS[0].Substring(3)

#Filling Array with Accountname
$DS = @( $DS -split " ")


$j=0
$KDS=@()
for ($j=0; $j -le ($DS.Length-1); $j++)
{
	if     ( $j -eq 0)            { $KDS = $DS[$j] }		  #No space at the beginning 	
	elseif ( $j -eq $DS.Length-1) { $KDS = $KDS + "`r`n" }    #CRLF at the end Ende
	else                          { $KDS = $KDS +" "+$DS[$j] }#Connect the names with spaces
	
	
#Array filling with AcountName
$User += $KDS
#Array filling with PN-PN-Number
$PNummer += $DS[-1]

}
$User = $User | sort
$PNummer1 = $PNummer | sort

You might elaborate a little more detailed what your actual problem is. It is not clear what is not working for you. You might share an example of the desired output as well. And please format all of your code as code. Thanks. :wink:

If I understand it right you should create your output objects completly first and sort them afterwards. It might help when you write your code a little more verbose … like Sort-Object -Property sAMAccountName for example.

If you feel more comfortable asking technical questions in your native language - there are some German Powershell forums availabe. Like

… or …
https://social.technet.microsoft.com/Forums/de-DE/home?forum=powershell_de

The following solution from Mike R. Many Thankx :slight_smile:

$Auswert = @(
‘CN= Bachhuber Hermann PN78945, DS, DA’
‘CN= Al Husainy Mohammed Wessam PN55710, DS, DA’
‘CN= Max Erwin Wundermann PN12345, DS, DA’
)

$results = @(Auswert | ForEach-Object { _ -match ‘CN= (.+)(PN[0-9]+)’ | Out-Null
#$_ -match ‘CN= (.+)(PN[0-9]+)’

[pscustomobject]@{
    Name = $Matches[1]
    PN   = $Matches[2]
}

})

How to sort a two Dimensional Array for each column
$results.Name
$results.PN | sort

This make my Day. :100: