Here is a sample script that allows you to configure a virtual machine to use either dynamic memory or static memory:

# Function for handling WMI jobs / return values
Function ProcessResult($result, $successString, $failureString)
{
   #Return success if the return value is "0"
   if ($result.ReturnValue -eq 0)
      {write-host $successString} 
 
   #If the return value is not "0" or "4096" then the operation failed
   ElseIf ($result.ReturnValue -ne 4096)
      {write-host $failureString "  Error value:" $result.ReturnValue}
 
   Else
      {#Get the job object
      $job=[WMI]$result.job
 
      #Provide updates if the jobstate is "3" (starting) or "4" (running)
      while ($job.JobState -eq 3 -or $job.JobState -eq 4)
         {write-host $job.PercentComplete "% complete"
          start-sleep 1
 
          #Refresh the job object
          $job=[WMI]$result.job}
 
       #A jobstate of "7" means success
       if ($job.JobState -eq 7)
          {write-host $successString}
       Else
          {write-host $failureString
          write-host "ErrorCode:" $job.ErrorCode
          write-host "ErrorDescription" $job.ErrorDescription}
       }
}
 
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
 
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
 
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
 
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
 
# SettingType = 3 ensures that we do not get snapshots
$SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.SettingType -eq 3}
 
# Get the memory setting data
$MemSetting = $SystemSettingData.getRelated("Msvm_MemorySettingData") | select -first 1
 
# Setup parameters for the prompt
$message = "Do you want to enable dynamic memory?"
$enable = New-Object System.Management.Automation.Host.ChoiceDescription "&Enable", "Enable dynamic memory on this virtual machine."
$disable = New-Object System.Management.Automation.Host.ChoiceDescription "&Disable", "Disable dynamic memory on this virtual machine."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($enable, $disable)
 
# Prompt for a response
$promptResult = $host.ui.PromptForChoice("", $message, $options, 0)
write-host 
 
switch ($promptResult)
   {
      0 {# Enable dynamic memory
      
         # Prompt for the memory settings
         $VirtualQuantity = Read-Host "Specify the startup memory for the virtual machine (MB)"
         $Limit = Read-Host "Specify the maximum memory for the virtual machine (MB)"
         $TargetMemoryBuffer = Read-Host "Specify the memory buffer for the virtual machine (5- 95)"
         $Weight = Read-Host "Specify the memory weight for the virtual machine (1 - 10000)"
      
         # Update the memory data setting object
         $MemSetting.DynamicMemoryEnabled = 1
         $MemSetting.Reservation = $VirtualQuantity
         $MemSetting.VirtualQuantity = $VirtualQuantity
         $MemSetting.Limit = $Limit
         $MemSetting.TargetMemoryBuffer = $TargetMemoryBuffer
         $MemSetting.Weight = $Weight        
        }
     
      1 {# Disable dynamic memory
      
         # Prompt for the memory settings
         $VirtualQuantity = Read-Host "Specify the amount of memory for the virtual machine (MB)"
         $Weight = Read-Host "Specify the memory weight for the virtual machine (1 - 10000)"
      
         # Update the memory data setting object
         $MemSetting.DynamicMemoryEnabled = 0
         $MemSetting.Reservation = $VirtualQuantity
         $MemSetting.VirtualQuantity = $VirtualQuantity
         $MemSetting.Limit = $VirtualQuantity
         $MemSetting.Weight = $Weight
        }
           
   }
 
# Apply the changes to the memory setting data back to the virtual machine
$result = $VMMS.ModifyVirtualSystemResources($VM, $MemSetting.GetText(1))
 
# Process the result
ProcessResult $result "Memory settings have been updated." "Failed to update memory settings."

One thing to highlight in this script is when the virtual machine is configured to have dynamic memory disabled, I still set the reservation and limit values (which are set to the same as the VirtualQuantity).  The reason for this is that even though dynamic memory is disabled – we still require that the VirtualQuantity be above the reservation and below the limit.  The easiest way to guarantee this when dynamic memory is disabled is to just set them all to the same value.

Cheers,
Ben