Sample


Please check below a sample of Resource monitoring PowerShell script.



Realtime CPU 


Realtime CPU control and measurement: Define win32_processor data with WMI object help and parameters.

$ComputerCPU = (Get-WmiObject  -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average


Realtime ram


Realtime ram control: Define a win32_operationgsystem data with WMI object help and parameters. Total visible memory size and free physical memory.

$ComputerMemory = Get-WmiObject -Class win32_operatingsystem -ErrorAction Stop

$UsedMemory = $ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory

$Memory = (($UsedMemory/ $ComputerMemory.TotalVisibleMemorySize)*100)

$RoundMemory = [math]::Round($Memory, 2)


Realtime disk


Realtime disk control: Define a win32_logicaldisk data with WMI object help and parameters.

#$disk = Get-PSDrive C | Select-Object Used,Free 

     

    $TotalDisk = Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'"  | ForEach-Object {[math]::truncate($_.Size / 1GB)} 

    $FreeSpace = Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'"  | ForEach-Object {[math]::truncate($_.freespace / 1GB)} 


Date format


Preparing date formats for correct output

$Date = Get-Date -DisplayHint Date -Format MM/dd/yyyy

  

    $Time = Get-Date -DisplayHint Time -Format HH:mm:ss

   Write-Host "Date: " $Date " Time: " $Time " CPU: " $ComputerCPU " Memory: " $RoundMemory " Free Space: " $Freespace " Total Size : " $TotalDisk 


All code


Run as administrator, PowerShell.  Copy and Paste code in a PowerShell console.

while($true)

{

    $ComputerCPU = (Get-WmiObject  -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average

 

    $ComputerMemory = Get-WmiObject  -Class win32_operatingsystem -ErrorAction Stop

    $UsedMemory = $ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory

    $Memory = (($UsedMemory/ $ComputerMemory.TotalVisibleMemorySize)*100)

    $RoundMemory = [math]::Round($Memory, 2)

     

    #$disk = Get-PSDrive C | Select-Object Used,Free 

     

    $TotalDisk = Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'"  | ForEach-Object {[math]::truncate($_.Size / 1GB)}

    $FreeSpace = Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'"  | ForEach-Object {[math]::truncate($_.freespace / 1GB)}

 

    $Date = Get-Date -DisplayHint Date -Format MM/dd/yyyy

 

    $Time = Get-Date -DisplayHint Time -Format HH:mm:ss

 

    Write-Host "Date: " $Date " Time: " $Time " CPU: " $ComputerCPU " Memory: " $RoundMemory " Free Space: " $Freespace " Total Size : " $TotalDisk

     

    sleep 2

}


while($true)
{
 
    $ComputerCPU = (Get-WmiObject  -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average
 
    $ComputerMemory = Get-WmiObject  -Class win32_operatingsystem -ErrorAction Stop
    $UsedMemory = $ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory
    $Memory = (($UsedMemory/ $ComputerMemory.TotalVisibleMemorySize)*100)
    $RoundMemory = [math]::Round($Memory, 2)
     
    #$disk = Get-PSDrive C | Select-Object Used,Free
     
    $TotalDisk = Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'"  | ForEach-Object {[math]::truncate($_.Size / 1GB)}
    $FreeSpace = Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'"  | ForEach-Object {[math]::truncate($_.freespace / 1GB)}
 
    $Date = Get-Date -DisplayHint Date -Format MM/dd/yyyy
 
    $Time = Get-Date -DisplayHint Time -Format HH:mm:ss
 
    Write-Host "Date: " $Date " Time: " $Time " CPU: " $ComputerCPU " Memory: " $RoundMemory " Free Space: " $Freespace " Total Size : " $TotalDisk
     
    sleep 2
}


Kaynak: microsoft.com