Add column to an output

Iron Contributor


I'm creating a list of HotFixes installed, to control another script that deploys the patches.
So I'm creating a command to check if an update has been installed on the last 10 days, plus if the server has been restarted, and now I want to output this into csv as well. But I need to add one columns to an output so I can make my life easier.
So this is my code:

Invoke-Command -ComputerName $server -Scriptblock {
       Get-HotFix | ?{$_.InstalledOn -gt [datetime]::Today.AddDays(-10)}
       Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime }

And this is the output:


But I need that the "lastbootuptime" is shown as a column above with the other data, so everything is together and I can export it to CSV.

How can I do this?

3 Replies
best response confirmed by dmarquesgn (Iron Contributor)





For a single additional column, you can just cram it into the Select-Object as shown below.


If you were going to work with multiple new or existing columns, you'd probably branch out into a dedicated hash table.



Invoke-Command -ComputerName $server -ScriptBlock {
    # Get the last boot time. Getting it here up front once rather than many times within the loop is more efficient.
    $LastBootTime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime;

    # Same principle for the cutoff date. No reason to have this in the loop.
    $CutoffDate = [datetime]::Today.AddDays(-10);

    # Loopy-da-loop time.
    Get-HotFix |
        Where-Object { $_.InstalledOn -gt $CutoffDate; } |
            Select-Object -Property @{name="LastBootUpTime"; expression={ $LastBootTime }}, CSName, Description, HotFixID, InstalledBy, InstalledOn;



Which gives you:




Note that the real variable name for Source is CSName. You can re-brand that if you like back to Source using the same approach as for the new LastBootUpTime column but I haven't bothered doing so in this example.




Thanks, I've learned a new thing and acomplished the goal.

 I'm especially stayed aware of the article and I will get many benefits from it. Subsequently, thank you for sharing it. DG Paystub Login