Posted On January 26, 2023

PowerShell: How to Change Guest VM Names in Virtual Machine Manager

kimconnect 0 comments
blog.KimConnect.com >> Virtualization >> PowerShell: How to Change Guest VM Names in Virtual Machine Manager
# changeGuessVmNameInVmm.ps1
# The following script prepend all VM's matching certain pattern
# This is useful to mark VM's to be deleted for ease of administration

$nameLike='test'
$prefix='_decom 02-01-2020_ '

$targetVms=Get-SCVirtualMachine|?{$_.Name -like "*$nameLike*"} 
foreach($targetVm in $targetVms){
    try{
        $oldName=$targetVm.Name
        $newName="$($prefix+$oldName)"
        $changed=Set-SCVirtualMachine -VM $targetVm -Name $newName
        if($changed.name -eq $newName){
            write-host "$oldName's name has been changed to $newName" -foregroundcolor Green
        }else{
            write-host "$oldName's name has NOT been changed to $newName" -foregroundcolor Red
        }
    }catch{
        write-warning $_        
    }    
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Connect to Azure CLI

# Set PSGallery as trusted to bypass promptsSet-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted -WarningAction SilentlyContinue #…

Kubernetes Ingress Error 502 Upon NextCloud Upgrades

Issue: Just the other day, I've attempted to run a 'helm upgrade…' command on my…

Hyper-V Logon Failure: the user has no been granted the requested logon type at this computer (0x80070569)

Issue: When performing a live migration of a guest VM, this error occurs: '...Failed to…