Hello Gazanfar,
You can use the following PS script cited from here by vipullag-MSFT:
#get all vms in a subscription
$vms = Get-AzVM -ResourceGroupName "ResourceGroupName"
#get the last 3 days data
#end date
$et=Get-Date
#start date
$st=$et.AddDays(-3)
$arr =@()
foreach($vm in $vms)
{
$s = ""
#percentage cpu usage
$cpu = Get-AzMetric -ResourceId $vm.Id -MetricName "Percentage CPU" -DetailedOutput -StartTime $st `
-EndTime $et -TimeGrain 12:00:00 -WarningAction SilentlyContinue
# 3 days == 72hours == 12*6hours
$cpu_total=0.0
foreach($c in $cpu.Data.Average)
{
#this is a average value for 12 hours, so total = $c*12 (or should be $c*12*60*60)
$cpu_total += $c*12
}
# add all the related info to the string
$s = "VM Name: " + $vm.name + "; Resource Group: " + $vm.ResourceGroupName + "; CPU: " +$cpu_total
# add the above string to an array
$arr += $s
}
#check the values in the array
$arr
Hopefully this helps you. You may need to edit some variables, but this grabs for all VMs in subscription's resource group. (this checks the last 3 days).
I have made adjustments to the code to include specifically for a Resource Group.
If this is helpful please accept answer.