At last week’s UK Powershell Usergroup Jonathan Noble was showing us some Powershell examples and at one point demonstrated something similar to the below.
Essentially you create an empty array, but then somewhat surprisingly (well to me anyway) you can select some elements even though they don’t exist! You are then able to add to the array using the names you have selected.
$MYInfo = "" | select-Object Name, CPUUsage,Owner, ProcessID $MYInfo.Name = $proc.name $MYInfo.ProcessID = $proc.IdProcess $MYInfo.CPUUsage = [Math]::Round($proc_perct, 0) $MYInfo.Owner = $process.GetOwner().user $myCol += $MYInfo
My friend and colleague Alan Renouf had given me the above example a week or so previously to use in a script. I was puzzled to how it worked since I hadn’t seen it used in any of the Powershell books I have read or other sources I use and neither had he, so when I saw Jonathan using it I was curious to find out more.
Jonathan explains in more detail how this works on his blog in a posting he made last year.
OK, it doesn’t really matter if it wasn’t him who come up with the original idea, but heh that’s how I’m going to remember it. :-)