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 add memory to 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.
Other articles in the series PowerShell Friday:
- PowerShell Friday: Getting Started with PowerShell and PowerCLI
- PowerShell Friday: Connecting to vCenter
- PowerShell Friday: Starting VMs
- PowerShell Friday: stopping VMs
- PowerShell Friday: Creating Virtual Machines
- PowerShell Friday: Snapshots
- PowerShell Friday: Adding CPU’s with PowerCLI
- PowerShell Friday: Adding Memory with PowerCLI
- PowerShell Friday: ExtensionData
- PowerShell Friday: Retrieving IP addresses for VMs
- PowerShell Friday: Copying files with Copy-VMGuestFile
- PowerShell Friday: Setting Reservations with PowerCLI
- PowerShell Friday: Enabling SSH with PowerCLI
- PowerShell Friday: Christmas Special
- PowerShell Friday: Configuring vSphere MTU Size
- PowerShell Friday: Load PowerCLI from your own script
- PowerShell Friday: Using the Cisco ACI API
- PowerShell Friday: Setting Reservations with PowerCLI by Anne Jan Elsinga
- PowerShell Friday: Adding CPU’s with PowerCLI by Anne Jan Elsinga
- PowerShell Friday: Snapshots by Anne Jan Elsinga
- PowerShell Friday: stopping VMs by Anne Jan Elsinga
- PowerShell Friday: Retrieving IP addresses for VMs by Anne Jan Elsinga
Anne Jan Elsinga
Anne Jan Elsinga is a Technical Account Manager for VMware. In the past he worked as presales and technical consultancy and architecture for several systems integrators. From 2009 until 2017 he was awarded with the VMware vExpert status. In the night time he dances latin, ballroom and salsa and he also discovered the pleasure of diving and woodworking. Recently he started blogging about smart homes and comfort in general.