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 http://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
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 scriptfile.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.
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
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
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
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
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
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
Send comments about this topic to Microsoft
Build date: 11/3/2009
<#.SYNOPSIS Determine if a computer screen saver requires a password..DESCRIPTION This script is a re-write of script 2 on the MSDN site (see below for link). This script also displays the user name for each desktop and the screen saver executable..NOTES File Name : Get-Screensaver.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3.LINK Script published to: http://www.pshscripts.blogspot.com Adapted from MSDN: http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx.EXAMPLE PS c:\foo> .\Get-Screensaver.ps1 5 desktops found as follows Desktop : NT AUTHORITY\SYSTEM Screen Saver : logon.scr Secure : False
Desktop : NT AUTHORITY\LOCAL SERVICE Screen Saver : %SystemRoot%\System32\logon.scr Secure : False
Desktop : NT AUTHORITY\NETWORK SERVICE Screen Saver : %SystemRoot%\System32\logon.scr Secure : False
Desktop : Cookham\tfl Screen Saver : %Systemroot%\tflscreensaver.scr Secure : False
Desktop : .DEFAULT Screen Saver : logon.scr Secure : False#>
### Start of script##
$Computer = "."$Desktops = Get-WMIObject -class Win32_Desktop -ComputerName $computer"{0} desktops found as follows" -f $desktops.countforeach ($desktop in $desktops) {"Desktop : {0}" -f $Desktop.Name"Screen Saver : {0}" -f $desktop.ScreensaverExecutable"Secure : {0} " -f $desktop.ScreenSaverSecure""}
# End of Script
<#.SYNOPSIS Displays Desktop Size.DESCRIPTION This script is a re-write of an MSDN sample. It uses the Win32_Desktop Monitor WMI class to obtain the screen dimensions..NOTES File Name : Get-DesktopResolution.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3.LINK Script Posted To: http://www.pshscripts.blogspot.com Re-write of sample 3 at: http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx.EXAMPLE PS c:\foo> .\Get-DesktopResolution There are 4 Desktops on UK0N055 as follows:
Desktop 1: Default Monitor Screen Height : 1200 Screen Width : 1920
Desktop 2: Default Monitor Screen Height : Screen Width :
Desktop 3: Default Monitor Screen Height : Screen Width :
Desktop 4: Default Monitor Screen Height : Screen Width :#>
# 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""}# End of Script
<#.SYNOPSIS Demonstrates uptime using WMI .DESCRIPTION This script used Win32_ComputerSystem to determine how long your system has been running. This is a rewrite of sample 3 at http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx. .NOTES File Name : Get-UpTime.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3.LINK Script Posted to: http://www.pshscripts.blogspot.com Original sample posted at: http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx.EXAMPLE PS c:\foo> .\Get-UpTime.Ps1 #>
# Helper Functionfunction 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} # End script