WMI Tasks: Desktop Management

WMI tasks for desktop management can exert control and obtain data from either a remote desktop or a local computer. For example, you can determine whether the screensaver on a local computer requires a password. WMI also gives you the ability shut down a remote computer. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

To run a script

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...determine if a remote computer has booted up in the Safe Mode with Networking state? Use the Win32_ComputerSystem class and check the value of the PrimaryOwnerName property.
VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    Wscript.Echo "System Name: " & objComputer.Name
    Wscript.Echo "Registered owner: " & objComputer.PrimaryOwnerName
Next
PowerShell
$colSettings = Get-WmiObject -Class Win32_ComputerSystem
foreach ($objComputer in $colSettings)
{
    "System Name: " + $objComputer.Name
    "Registered owner: " + $objComputer.PrimaryOwnerName
}
...determine if a computer screensaver requires a password?

Use the Win32_Desktop class and check the value of the ScreenSaverSecure property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Desktop")
For Each objItem in colItems
    Wscript.Echo "Screen Saver Secure: " & objItem.ScreenSaverSecure
Next
PowerShell
$Computer = "."
$Desktops = Get-WMIObject -class Win32_Desktop -ComputerName $computer
"{0} desktops found as follows" -f $desktops.count
foreach ($desktop in $desktops) {
     "Desktop      : {0}"  -f $Desktop.Name
     "Screen Saver : {0}"  -f $desktop.ScreensaverExecutable
     "Secure       : {0} " -f $desktop.ScreenSaverSecure
     ""
}
...verify that a computer screen has been set for 800 pixels by 600 pixels?

Use the Win32_DesktopMonitor class and check the values of the properties ScreenHeight and ScreenWidth.

VB
strComputer = "."
Set objWMIService = GetObject(_
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")
For Each objItem in colItems
    Wscript.Echo "Screen Height: " & objItem.ScreenHeight
    Wscript.Echo "Screen Width: " & objItem.ScreenWidth
Next

PowerShell
<# get="" desktop="" information="" #="">
$computer = "."
$desktops = Get-WmiObject -Class Win32_DesktopMonitor
$hostname = hostname

<# Display desktop details #> "There are {0} Desktops on {1} as follows:" -f $desktops.Count, $hostname "" $i=1 # count of desktops on this system

foreach ($desktop in $desktops) { "Desktop {0}: {1}" -f $i++, $Desktop.Caption "Screen Height : {0}" -f $desktop.ScreenHeight "Screen Width : {0}" -f $desktop.ScreenWidth "" }

...determine how long a computer has been running?

Use the Win32_OperatingSystem class and the LastBootUpTime property. Subtract that value from the current time to get the system uptime.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
 
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootUpTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
    Wscript.Echo dtmSystemUptime 
Next
 
Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate =  CDate(Mid(dtmBootup, 5, 2) & "/" & _
        Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) & " " & Mid (dtmBootup, 9, 2) & ":" & _
        Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, 13, 2))
End Function

PowerShell
function WMIDateStringToDate($Bootup) {
[System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
}

<# Main script #> $Computer = "." # adjust as needed $computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer

foreach ($system in $computers) { $Bootup = $system.LastBootUpTime $LastBootUpTime = WMIDateStringToDate($Bootup) $now = Get-Date $Uptime = $now-$lastBootUpTime "System Up for: {0}" -f $UpTime }

...reboot or shut down a remote computer?

Use the Win32_OperatingSystem class and the Win32Shutdown method. You must include the RemoteShutdown privilege when connecting to WMI. For more information, see Executing Privileged Operations Using VBScript. Unlike the Shutdown method on Win32_OperatingSystem, the Win32Shutdown method allows you to set flags to control the shutdown behavior.

VB
strComputer = "atl-dc-01"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Shutdown(1)
Next
PowerShell
strComputer = "atl-dc-01"
$colOperatingSystem = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $strComputer -Namespace "wmi\cimv2"
foreach ($objOperatingSystem in $colOperatingSystem)
{
    [void]$objOperatingSystem.Shutdown()
}
...determine what applications automatically run each time I start Windows?

Use the Win32_StartupCommand class.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colStartupCommands = objWMIService.ExecQuery _
    ("Select * from Win32_StartupCommand")
For Each objStartupCommand in colStartupCommands
    Wscript.Echo "Command: " & objStartupCommand.Command & VBNewLine _
    & "Description: " & objStartupCommand.Description & VBNewLine _
    & "Location: " & objStartupCommand.Location & VBNewLine _
    & "Name: " & objStartupCommand.Name & VBNewLine _
    & "SettingID: " & objStartupCommand.SettingID & VBNewLine _
    & "User: " & objStartupCommand.User
Next
PowerShell
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_StartupCommand -ComputerName $strComputer
foreach ($objStartupCommand in $colItems) 
{ 
    "Command: " + $objStartupCommand.Command
    "Description: " + $objStartupCommand.Description 
    "Location: " + $objStartupCommand.Location 
    "Name: " + $objStartupCommand.Name 
    "SettingID: " + $objStartupCommand.SettingID 
    "User: " + $objStartupCommand.User
}

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter