You can use the Rename method to rename a computer if you are a member of the local administrator group. However, you cannot use the method remotely for domain computers.
If the Password and UserName parameters are specified, the connection to WMI must use the RPC_C_AUTHN_LEVEL_PKT_PRIVACY (wbemAuthenticationLevelPktPrivacy for script and Visual Basic (VB)) authentication level.
To connect to a remote computer and specify credentials, use the locator object connection, which is IWbemLocator for C++, and SWbemLocator for script and VB. Do not use the moniker connection.
To connect to a local computer, you cannot specify a password or an authentication authority, such as Kerberos. You can only specify the password and authority in connections to remote computers.
If the authentication level is too low when a Password and UserName are specified, then WMI returns the WBEM_E_ENCRYPTED_CONNECTION_REQUIRED error for C/C++, and wbemErrEncryptedConnectionRequired for script and VB.
For more information, see SWbemLocator_ConnectServer,IWbemLocator::ConnectServer, and Constructing a Moniker String.
The following script shows you how to rename a local computer.
The following C++ code sample renames a computer system.
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)]
[String]
$Name,
[Parameter(Mandatory)]
[String]
$Password,
[Parameter(Mandatory)]
[String]
$UserName
)
$query = 'Select * From Win32_ComputerSystem'
Invoke-CimMethod -Query $query -MethodName Rename -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)]
[String]
$Name,
[Parameter(Mandatory)]
[String]
$Password,
[Parameter(Mandatory)]
[String]
$UserName,
[String[]]
$ComputerName,
[PSCredential]
$Credential
)
$session = New-CimSession -ComputerName $ComputerName -Credential $Credential
$query = 'Select * From Win32_ComputerSystem'
Invoke-CimMethod -Query $query -MethodName Rename -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 Rename(
[in] string Name,
[in] string Password,
[in] string UserName
);
Parameters
Name | Type | Description | |
---|---|---|---|
Name | String | The Name parameter specifies the new name. The name cannot include control characters, leading or trailing spaces, or any of the following characters: / \ [ ] : | < > + = ; , ? |
Password | String | If the UserName parameter specifies an account name, the Password parameter must point to the password to use when connecting to the domain controller. Otherwise, this parameter must be NULL. Password and Username must use a high authentication level (i.e. no less than RPC_C_AUTHN_LEVEL_PKT_PRIVACY) when they connect to winmgmt (i.e. in the call to obtain the IWbemServices interface) or SetProxyBlanket on their IWbemServices ptr. If they are local to winmgmt then this is not a concern because their authentication level is as good as RPC_C_AUTHN_LEVEL_PKT_PRIVACY as well as their client request never goes over the wire to get to the winmgmt. If Password and Username are left null, the provider does not care. If the provider determines that the authentication level is too low and a Password or Username has been specified then WBEM_E_ENCRYPTED_CONNECTION_REQUIRED will be returned. This parameter is only used for domain renames on Windows 2000 platforms and beyond. |
|
UserName | String | The Username parameter is a pointer to a constant null-terminated character string that specifies the account name to use when connecting to the domain controller. The string must specify either a domain NetBIOS name and user account (for example, “REDMOND\user”) or the user principal name (UPN) of the user in the form of an Internet-style login name (for example, “[email protected]”). If this parameter is NULL, the caller’s context is used. Password and Username must use a high authentication level (i.e. no less than RPC_C_AUTHN_LEVEL_PKT_PRIVACY) when they connect to winmgmt (i.e. in the call to obtain the IWbemServices interface) or SetProxyBlanket on their IWbemServices ptr. If they are local to winmgmt then this is not a concern because their authentication level is as good as RPC_C_AUTHN_LEVEL_PKT_PRIVACY as well as their client request never goes over the wire to get to the winmgmt. If Password and Username are left null, the provider does not care. If the provider determines that the authentication level is too low and a Password or Username has been specified then WBEM_E_ENCRYPTED_CONNECTION_REQUIRED will be returned. This parameter is only used for domain renames on Windows 2000 platforms and beyond. |
Return Value
Returns a value of type UInt32. Return values:
$returnValues = @{
0 = 'Success'
}
See Also
Additional methods implemented by Win32_ComputerSystem:
JoinDomainOrWorkgroup()
JoinDomainOrWorkgroup() adds a computer system to a domain or workgroup.
SetPowerState()
SetPowerState() not implemented.
UnjoinDomainOrWorkgroup()
UnjoinDomainOrWorkgroup() removes a computer system from a domain or workgroup.
Requirements
To use Win32_ComputerSystem, 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_ComputerSystem was introduced on clients with Windows Vista and on servers with Windows Server 2008.
Namespace
Win32_ComputerSystem 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_ComputerSystem 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