PowerShell Friday: Adding Memory with PowerCLI
Just before the holidays we’ve got a small PowerCLI onliner for you. Nothing fancy, but still useful.
There are times that your virtual machine doesn’t have enough memory. Yes, you can go into the vSphere Client to change the memory configuration, but where is the fun in that? Why not do everything from the command line? And preferably by the use of PowerCLI?
This oneliner will set the memory of a specific virtual machine.
Get-VM -Name MyVM | Set-VM -MemoryGB 2
You get prompted if you’re sure that you want to change the VM.
If you want to specifiy less than a GB you still use the -MemoryGB parameter, but with a decimal:
Get-VM -Name MyVM |Set-VM -MemoryGB 0.75
This cmdlet will give your virtual machine 768 MB memory.
Before adding memory with PowerCLI
Of course you want to know how much memory your virtual machine has before you add something to it. To accomplish this you use the following:
Get-VM -Name MyVM
Or, if you want a list of all VMs with their names and memory configuration:
Get-VM -Name MyVM | FT Name, MemoryGB
How about reducing memory?
Ofcourse, you also can reduce memory, just set the memory to something lower than the machine already has. For this the machine has to be powered off
Get-VM -Name MyVM | Shutdown-VMGuest|Set-VM -MemoryGB 0.25
This online shuts down the virtual machine and set its memory to 256MB.
Some tricks with PowerCLI and Memory
Now that you can get and change the memory of a virtual machine from the command line you can do all kinds of crazy things with it. With one command you can double the memory
set-vm -VM MyVM -MemoryGB ((Get-VM -Name MyVM).MemoryGB * 2)
Or set the memory of one VM to the same value as another VM.
Set-VM -VM MyVM -MemoryGB (Get-VM -Name MyVM2).MemoryGB
The Sky is the limit.