Windows XP introduced several WMI classes and a scripting object to parse or convert the CIM datetime format. 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.
Windows 2000: SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
Set dtmInstallDate = CreateObject( _ "WbemScripting.SWbemDateTime") strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set objOS = objWMIService.ExecQuery( _ "Select * from Win32_OperatingSystem") For Each strOS in objOS dtmInstallDate.Value = strOS.InstallDate Wscript.Echo dtmInstallDate.GetVarDate Next
Windows 2000: Win32_LocalTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_LocalTime") For Each objItem in colItems Wscript.Echo "Day: " & objItem.Day & VBNewLine _ & "Day Of Week: " & objItem.DayOfWeek & VBNewLine _ & "Hour: " & objItem.Hour & VBNewLine _ & "Minute: " & objItem.Minute & VBNewLine _ & "Month: " & objItem.Month & VBNewLine _ & "Quarter: " & objItem.Quarter & VBNewLine _ & "Second: " & objItem.Second & VBNewLine _ & "Week In Month: " & objItem.WeekInMonth & VBNewLine _ & "Year: " & objItem.Year Next
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_TimeZone") For Each objItem in colItems Wscript.Echo "Description: " & objItem.Description Wscript.Echo "Daylight Name: " & objItem.DaylightName Wscript.Echo "Standard Name: " & objItem.StandardName Wscript.Echo Next
Send comments about this topic to Microsoft
Build date: 11/3/2009
<# .SYNOPSIS Gets time from WMI and displays it .DESCRIPTION This script gets Win32_LocalTime and then displays the details. .NOTES File Name : Get-LocalDateTime.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK Script postesd to: http://www.pshscripts.blogspot.com MSDN Sample at:http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx.EXAMPLE [ps] c:\foo> .\Get-WMILocaltime.ps1 Day : 26 Day Of Week : 1 Hour : 20 Minute : 59 Month : 1 Quarter : 1 Second : 53 Week In Month: 5 Year : 2009 #> ### # Start of Script ## # Speficy computer and get Local Time $Computer = "." $times = Get-WmiObject Win32_LocalTime -computer $computer # Now display the result Foreach ($time in $times) { "Day : {0}" -f $Time.Day "Day Of Week : {0}" -f $Time.DayOfWeek "Hour : {0}" -f $Time.Hour "Minute : {0}" -f $Time.Minute "Month : {0}" -f $Time.Month "Quarter : {0}" -f $Time.Quarter "Second : {0}" -f $time.Second "Week In Month: {0}" -f $Time.WeekInMonth "Year : {0}" -f $Time.Year } # End of Script
<# .SYNOPSIS Gets and displays time zone information via WMI .DESCRIPTION This script uses the Win32_TimeZone class to obtain, then display time zone information for a computer. This script is a MSDN sample recoded in PowerShell .NOTES File Name : Get-TimeZoneInfo.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK Script postesd to: http://pshscripts.blogspot.com/2009/01/get-timezoneinfops1.html MSDN Sample at: http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx .EXAMPLE [ps] c:\foo> .\Get-TimeZoneInfo.ps1 Time zone information on computer "Win7" Time Zone Description : (UTC) Dublin, Edinburgh, Lisbon, London Daylight Name : GMT Daylight Time Standard Name : GMT Standard Time #> ### # Start of Script ## # Get Time Zone on a computer $Computer = "." $timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $computer # Display details if ($computer -eq ".") {$computer = Hostname} "Time zone information on computer `"{0}`"" -f $computer "Time Zone Description : {0}" -f $timezone.Description "Daylight Name : {0}" -f $timezone.DaylightName "Standard Name : {0}" -f $timezone.StandardName #End of Script