PowerShell - Hyper-V - Adding comments to a virtual machine

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

To keep track of changes to virtual machines in Hyper-V, it can be useful to use the comments function and add any information you think might be useful.

Here’s a little PowerShell function for adding comments easily.
The comments will take the following form [date] comment (user) :

[2024-04-01_13:20:35] Comment test (Guillaume)
[2024-04-02_10:24:49] Nested virtualisation configuration (Guillaume)

function Add-VMNoteGB {
    [CmdletBinding()]
    param (
        [string]$VmName,
        [string]$ComputerName = $env:COMPUTERNAME,
        [string]$Note
    )
  
    begin {
        [string]$ActualNote = (get-vm $VmName -ComputerName $ComputerName).Notes
    }
  
    process {
        [string]$NewNote = '[{0:yyyy-MM-dd_HH:mm:ss}] {2} ({1})' -f (get-date), $env:USERNAME, $Note
        [string]$FinalNote = $ActualNote, $NewNote -join "`n" 
        Set-VM -VMName $VmName -Notes $FinalNote -ComputerName $ComputerName
    }
    
}
Add-VMNoteGB -VmName VM1 -Note 'Test de commentaire'
# Or for a virtual machine on a remote hyper-v server
Add-VMNoteGB -VmName VM1 -Note 'Comment test' -ComputerName srv-hyperv-1

# Display comments
(Get-Vm -VmName VM1) Notes

Related links