i use this powershell command:
get-vm | ft name, *start*, *stop*, customproperties
that returns objects with a string array as a property (customproperties):
Name StartAction DelayStart StopAction CustomProperties
---- ----------- ---------- ---------- ----------------
TKAD4 AlwaysAutoTurnOnVM 0 ShutdownGuestOS {NoStartupDelay, ...
TKAD3 AlwaysAutoTurnOnVM 0 ShutdownGuestOS {NoStartupDelay, ...
how can i return just one element from an array the is a property as an object to display it as part of a table?
my desired output would look like this:
Name StartAction DelayStart StopAction Custom1
---- ----------- ---------- ---------- -------
TKAD4 AlwaysAutoTurnOnVM 0 ShutdownGuestOS NoStartupDelay
TKAD3 AlwaysAutoTurnOnVM 0 ShutdownGuestOS NoStartupDelay
The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.
To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator ( = ). The comma can also be used to initialize a single item array by placing the comma before the single item.
If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.
To add value to the array, you need to create a new copy of the array and add value to it. To do so, you simply need to use += operator. For example, you have an existing array as given below. To add value “Hello” to the array, we will use += sign.
In your Format-Table, change customproperties
to either:
@{label='Custom1';e={$_.CustomProperties[0]}}
If it is an array. If it is a collection use:
@{label='Custom1';e={$_.CustomProperties | Select -First 1}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With