Remove Computer - PowerShell


Overview

Remove-Computer removes a computer object from Active Directory. This is useful when a computer is no longer in use or has been decommissioned.

Syntax

Remove-Computer [-ComputerName] <String[]> [-DomainCredential <PSCredential>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Options/Flags

  • -ComputerName: Specifies the name of the computer object to be removed. You can specify multiple computer names. The default value is the local computer’s name.
  • -DomainCredential: Specifies the user credentials to use when removing the computer object from Active Directory. The default value is the current user’s credentials.
  • -Force: Specifies whether to remove the computer object even if it is currently in use. The default value is $false.
  • -WhatIf: Specifies whether to display what would happen if the command were run without actually performing the action. The default value is $false.
  • -Confirm: Specifies whether to prompt the user for confirmation before removing the computer object. The default value is $false.

Examples

Example 1: Remove a single computer object

Remove-Computer -ComputerName "computer1"

Example 2: Remove multiple computer objects

Remove-Computer -ComputerName "computer1", "computer2", "computer3"

Example 3: Remove a computer object using domain credentials

$credentials = Get-Credential -Domain "example.com"
Remove-Computer -ComputerName "computer1" -DomainCredential $credentials

Example 4: Remove a computer object that is currently in use

Remove-Computer -ComputerName "computer1" -Force

Common Issues

  • The computer object does not exist in Active Directory.
  • The user does not have sufficient permissions to remove the computer object.
  • The computer object is currently in use.

Integration

Remove-Computer can be used in conjunction with other PowerShell commands to automate the removal of computer objects from Active Directory. For example, the following script removes all computer objects that are not in use:

$computers = Get-ADComputer | Where-Object {$_.Enabled -eq $false}
Remove-Computer -ComputerName $computers
  • Add-Computer
  • Get-Computer
  • Disable-Computer