How to re-create symlinks of VM configs in Hyper-V using PowerShell

  • Hyper-V keeps VM config at %ProgramData%\Microsoft\Windows\Hyper-V\Virtual Machines\ as a symbolic link (aka symlink) to the original location.

You may get them broken due to various reasons, e.g.:

  • Server disaster and then recovery
  • Upgrade to the next version of OS/Hyper-V and then rollback
  • Migration of virtual machine

To restore functioning you need to create a symlink for each xml config, i.e.:

mklink %ProgramData%\Microsoft\Windows\Hyper-V\Virtual Machines\{guid}.xml d:\MyVM\Virtual Machines\{guid}.xml

But how to automate this if you have tens of VMs? Here’s the command:

Get-ChildItem -Recurse *.xml | `
New-Symlink -LiteralPath { Join-Path -Path '%ProgramData%\Microsoft\Windows\Hyper-V\Virtual Machines' -ChildPath $_.Name } -TargetPath { $_.FullName }
This entry was posted in Infrastructure and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.