SetDateTime

Allows the computer date and time to be set.

The calling process must have the SE_SYSTEMTIME_NAME privilege.

Example

Do not run below example code just to see what happens next. Many methods seriously affect your system. Always make sure you actually understand what the method and the code do.

param
(
  [Parameter(Mandatory)]
  [DateTime]
  $LocalDateTime
)


$query = 'Select * From Win32_OperatingSystem'

Invoke-CimMethod -Query $query -MethodName SetDateTime -Arguments $PSBoundParameters |
  Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Success'}
        default  {"Unknown Error $_"}
    }
}

To run this method on one or more remote systems, use New-CimSession:

param
(
  [Parameter(Mandatory)]
  [DateTime]
  $LocalDateTime,

  [String[]]
  $ComputerName,

  [PSCredential]
  $Credential
)


$session = New-CimSession -ComputerName $ComputerName -Credential $Credential

$query = 'Select * From Win32_OperatingSystem'

Invoke-CimMethod -Query $query -MethodName SetDateTime -Arguments $PSBoundParameters -CimSession $session |
  Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Success'}
        default  {"Unknown Error $_"}
    }
}


Remove-CimSession -CimSession $session

Learn more about Invoke-CimMethod and invoking WMI methods.

Syntax

uint32 SetDateTime(
  [in] datetime LocalDatetime
);

Parameters

Name Type Description
LocalDateTime DateTime The LocalDateTime parameter is the time to set. This property may not be NULL.

Return Value

Returns a value of type UInt32. Return values:

$returnValues = @{
    0    = 'Success'
}

See Also

Additional methods implemented by Win32_OperatingSystem:

Reboot()

Reboot() shuts down and then restarts the computer system.

Shutdown()

Shutdown() unloads programs and DLLs to the point where it is safe to turn off the computer.

Win32Shutdown()

Win32Shutdown() provides the full set of shutdown options supported by Windows operating systems.

Win32ShutdownTracker()

Win32ShutdownTracker() provides the same set of shutdown options supported by Win32Shutdown() but also allows you to specify comments, a reason for shutdown, or a timeout.

Requirements

To use Win32_OperatingSystem, the following requirements apply:

PowerShell

Get-CimInstance was introduced with PowerShell Version 3.0, which in turn was introduced on clients with Windows 8 and on servers with Windows Server 2012.

If necessary, update Windows PowerShell to Windows PowerShell 5.1, or install PowerShell 7 side-by-side.

Operating System

Win32_OperatingSystem was introduced on clients with Windows Vista and on servers with Windows Server 2008.

Namespace

Win32_OperatingSystem lives in the Namespace Root/CIMV2. This is the default namespace. There is no need to use the -Namespace parameter in Get-CimInstance.

Implementation

Win32_OperatingSystem is implemented in CIMWin32.dll and defined in CIMWin32.mof. Both files are located in the folder C:\Windows\system32\wbem:

explorer $env:windir\system32\wbem
notepad $env:windir\system32\wbem\CIMWin32.mof