PSObject obj = new PSObject(); obj.Properties.Add(new PSNoteProperty("Name", "John")); obj.Properties.Add(new PSNoteProperty("Age", 35)); obj.Properties.Add(new PSNoteProperty("Gender", "Male")); foreach (PSPropertyInfo property in obj.Properties) { Console.WriteLine(property.Name + ": " + property.Value); }
PSObject obj1 = new PSObject(); obj1.Properties.Add(new PSNoteProperty("Name", "John")); obj1.Properties.Add(new PSNoteProperty("Age", 35)); PSObject obj2 = new PSObject(); obj2.Properties.Add(new PSNoteProperty("Name", "Mary")); obj2.Properties.Add(new PSNoteProperty("Age", 27)); ListThis code creates two PSObjects (representing two people) and then adds them to a list. The code then loops through the list to print out each person's name and age. The System.Management.Automation namespace is part of the .NET Framework and can be found in the Microsoft.PowerShell.ConsoleHost assembly.people = new List (); people.Add(obj1); people.Add(obj2); foreach (PSObject person in people) { Console.WriteLine(person.Properties["Name"].Value + " is " + person.Properties["Age"].Value + " years old."); }