How to work with the next XML element within a list of elements

Hi all,

me again :slight_smile:

I have a XML looking like this (I changed the name of the elements):

Gist Link

I will now check the XML for the usage of ApplicationC. After filtering all XML files with an IF statement I will grab the ModifiedDate from the first appearance of this entry, which is working

$date1 = $xml.Case.ChangeHistory.ChangeHistoryItem | ? { $_.ApplicationName -eq "ApplicationC" } | % { $_.ModifiedDate } | select -First 1

I can also grab the ModifiedDate of the last appearance of the App

$date2 = $xml.Case.ChangeHistory.ChangeHistoryItem | ? { $_.ModifiedDate } | % { $_.ModifiedDate } | select -Last 1

But what I also need is the following:
Search for appearance of ApplicationC > ok
Grab the ModifiedDate of first appearance > ok
Grab the ModifiedDate of the next ChangeHistoryItem after the first appearance of ApplicationC > not working

How can I jump to the next element in the list of ChangeHistoryItem elements?

Background: The ModifiedDate of the next element (no matter which App it is) minus the ModifiedDate of the first appearance of ApplicationC will provide the usage time of ApplicationC.

Thanks a lot!

Best regards,

Here you go,

$xml.Case.ChangeHistory.ChangeHistoryItem | Where-Object -FilterScript {$_.ApplicationName -eq 'ApplicationC'} |Select-Object -Skip 1 -First 1

Here Select-Object -Skips first one and Select the first from remaining.

It may be over the top, but if I understand you correctly you want to extract each occurence of ‘ApplicationC’ with its ‘ModiefiedData’ together with the following element from the list, right? This could be an approach:

$index = 0
$ListWithIndex = $xml.Case.ChangeHistory.ChangeHistoryItem |
ForEach-Object {
Index = index ++ ApplicationName = .ApplicationName
ModifiedDate = $
foreach ($Element in $ListWithIndex){
If ($Element.ApplicationName -eq ‘ApplicationC’) {
ApplicationName = $Element.ApplicationName
ModifiedDate = $Element.ModifiedDate
FollowingApplication = $ListWithIndex.ApplicationName[$Element.Index + 1]
FollowingModifiedData = $ListWithIndex.ModifiedDate[$Element.Index + 1]

If you convert the ModifiedDate to DateTime objects you could calculate the timespan between the occurence of ‘ApplicationC’ and the following ApplicationName to determine the usage time.