0

I have a Projects class defined as below:

class Projects{
    [string]$Name;
    [string]$FolderName;
    [string]$RemoteRepo;
    }
}

Then I create a hashtable:

$Prj  = @{"A1" = [Projects]@{Name = 'A1'; FolderName = 'B1'; RemoteRepo = 'C1'}}
$Prj += @{"A2" = [Projects]@{Name = 'A2'; FolderName = 'B2'; RemoteRepo = 'C2'}}
$Prj += @{"A3" = [Projects]@{Name = 'A3'; FolderName = 'B3'; RemoteRepo = 'C3'}}
$Prj += @{"A4" = [Projects]@{Name = 'A4'; FolderName = 'B4'; RemoteRepo = 'C4'}} 

Then, for every value in the Prj hashtable, I want to extract the FolderName value, add a constant string let say "www" to it and display it.

To do so I'm doing like that:

ForEach ($p in $Prj)
{
    ($p.Values).FolderName + " www"
}

Instead of expected result:

B1 www
B2 www
B3 www
B4 www

I'm getting this:

B1
B2
B3
B4
 www

My questions:

  1. Why?
  2. How to do to get the expected result.
user1146081
  • 195
  • 15
  • 1
    As an aside: [try to avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) – iRon Aug 03 '22 at 09:10
  • 1
    [member-access enumeration](https://stackoverflow.com/a/44620191/45375) allows you to access a property on a collection and have the property values from the individual elements returned. ***However*, that only works for getting properties, not for setting them.** See: [PowerShell update nested value](https://stackoverflow.com/a/51994110/1701026) – iRon Aug 03 '22 at 11:33
  • 1
    `$Prj.Values.foreach{ $_.FolderName += " www" }` – iRon Aug 03 '22 at 11:38

0 Answers0