The below script guides you to add/remove a vmss extension in Azure. The example is for adding Microsoft Monitoring Agent to Azure vmss which will have workspace Id to collect custom logs from VM's
Get VMSS to a variable
$vmssname= Get-AzVmss -ResourceGroupName npqatx -VMScaleSetName <vmssname>
$workspaceId="GUID of log analytics workspace"
$workspaceKey="workspace key"
$settings=@{"workspaceId" = $workspaceId}
$protSettings=@{"workspaceKey" = $workspaceKey}
#Adding new extension
Add-AzVmssExtension -VirtualMachineScaleSet $vmssname -Name "MMAExtension" -Publisher "Microsoft.EnterpriseCloud.Monitoring" -Type "MicrosoftMonitoringAgent" -TypeHandlerVersion "1.0" -AutoUpgradeMinorVersion $true -Setting $settings -ProtectedSetting $protSettings
Update-AzVmss -ResourceGroupName "RGName" -VMScaleSetName $vmssname.Name -VirtualMachineScaleSet $vmssname
#Remove existing vmss extension
Remove-AzVmssExtension -VirtualMachineScaleSet $webvmss -Name "MMAExtension"
Update-AzVmss -ResourceGroupName "RGName" -VMScaleSetName $vmssname.Name -VirtualMachineScaleSet $vmssname
#make sure to upgrade instances in vmss to the latest model after every change to vmss.