Functions/Move-MyVMStorage.ps1
function Move-MyVMStorage { param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [ArgumentCompleter( { param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters ) Get-VM "$wordToComplete*" | Sort-Object Name | ForEach-Object { "`"$($_.Name)`"" } })] [string[]] $VMName, [Parameter(Mandatory = $true)] [string] $DestinationPath ) $VMs = Get-VM $VMName -ErrorAction 0 -Verbose | Select-Object * if ($VMs) { foreach ($VM in $VMs) { $NewPath = Join-Path $($DestinationPath) $($VM.Name) # $VM.Path # $NewPath if ($VM.Path -eq $NewPath) { Write-Warning "VM $($VM.Name) already in this location" } else { Move-VMStorage -VMName $VM.Name -DestinationStoragePath $NewPath -Verbose if ((Get-ChildItem $VM.Path -Recurse -File).count -eq 0) { Remove-Item $VM.Path -Recurse -Force } else { Write-Warning "Folder $($VM.Path) not empty" } } } } else { Write-Warning "No VM`'s found" } } |