'Sitecore Powershell - How to get one or multiple Multlist Value

I have a little bit of confusion regarding the Multilist Field in Sitecore. Basically, I want to get all of the selected MultiList Field in PowerShell to do stuff with them.

Is it possible to make those values in array?

Here is the code so far:

$props = @{
    Title = "Get All Media Items by Tag"
    Description = "Get all Media items by Tag"
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(
        @{ Name = "TagName"; Title = "Tag Name"; Editor = "multilist"; Source = "/sitecore/system/Settings/Buckets/TagRepository/2021/12/16/14/36" }
        @{ Title = "Note"; Value = "Select Tags you need for the Images from the Tag Repository"; Editor = "info" }
    )
}

$result = Read-Variable @props
if($result -ne "ok") {
    Close-Window
    Exit
}

And this is the picture showing what this code does:

enter image description here

Is there any way to get those selected values and make them in array, or they are in array by default?

I personally think maybe $TagName.Values would do the trick, or something like that



Solution 1:[1]

You can find the selected tags assigned to the TagName parameter that you have defined.

Below script can help you to fetch the DISPLAY NAME and KEY of the selected tags in an array and also print them

$props = @{
    Title = "Get All Media Items by Tag"
    Description = "Get all Media items by Tag"
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(
        @{ Name = "TagName"; Title = "Tag Name"; Editor = "multilist"; Source = "/sitecore/system/Settings/Buckets/TagRepository/2021/12/16/14/36" }
        @{ Title = "Note"; Value = "Select Tags you need for the Images from the Tag Repository"; Editor = "info" }
    )
}

$result = Read-Variable @props

#The parameter TagName will hold the selected values
#Below line is a sample console to print the number of Selected Tags
write-host "No of Tags Selected : $($TagName.Count)"

#declaring empty array to hold the values
$tagDisplayName = @()
$tagKey = @()

#iterate through the selected tags
$TagName | ForEach-Object { 
     #fetch the DISPLAY NAME and KEY field value to its array variable
     $tagDisplayName += $_.DisplayName
     $tagKey += $_.key
}

#Print the array object values of the Selected Tags
write-host "Display Name of the Selected Tags are --> $tagDisplayName"
write-host "Key Field Value of the Selected Tags are --> $tagKey"

if($result -ne "ok") {
    Close-Window
    Exit
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gajalakshmi Govindarajan