I have two scripts namely, Add and Report. I am having a array of items in Add script. I want to read this array in Report script. Add script can be called multiple times. I want to retain the value in array and read them in Report script. I have added Persistent Session ID to both these scripts. How do I update the array list in Add script whenever the add script is called and read the array list in Report script. I have a global function where I have initialized the class,

class Modes {
[Modes[]]$itemModes = @()

In Add.script,

foreach($item in $selectedItems){
                    $itemMode= [Modes]@{
                    ItemId =  (Get-Item $item).Name
                    InstallMode= $installModeOption
                    MergeMode =  $mergeModeOption
                    $itemModes =[Array] $itemModes +  $itemMode

In Report.script


How do I read the itemsModes I have added in Add.script in Report.script?

  • When you try to access the $itemModes, in another script, did you get any error? Because I created two scripts. In the first one, I assigned a list of items to a variable, and when I access that in another script it shows the output. May 23, 2022 at 13:50
  • Hi Sumit, I think this line is causing the issue,ItemId = (Get-Item $item).Name. I need to get the ID of each item. Bt this is not working as expected. So error is thrown May 23, 2022 at 14:13
  • If you are getting $item in foreach loop then why you are trying to use Get-Item? Can't you just use $item.Name? May 23, 2022 at 14:25
  • Initially I tried with this one. It was throwing some error. So I used Get-Item. Now I have updated it to $item.Name. it's working fine and error is now gone. May 23, 2022 at 14:36
  • So did it resolve your issue? May 23, 2022 at 14:44

1 Answer 1


As per this discussion, When you have items in your $selectedItems and then in the loop, you are again trying to call Get-Item is creating the issue. You just need to update

ItemId =  (Get-Item $item).Name


ItemId =  $item.Name

And it will solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.