Avatar of AkermanIT
AkermanIT

asked on 

Remove-ADComputer -Filter question

I need a script to remove computers from Active Directory based an input file with computer names. Here's the kicker, the last two characters of the computer name could either be a "-P" or a "-W", so my input files contains only the base computer name without the "-P" or "-W".  How do I pass computername-* to Remove-ADComputer?

From my research I gather a scriptblock might part of the answer, as per this: https://social.technet.microsoft.com/forums/windowsserver/en-US/8bcca597-e7ef-4eef-af5d-4cef1db4f7bc/error-variable-found-in-expression-is-not-defined. However, I have not been able to get it to work. Here's what I have so far:

$Count = 0
$Filter = [ScriptBlock]::Create({Name -like $($Computer.computer)+"-*"})
$Computers = Import-CSV "C:\Input.csv" 

ForEach ($Computer in $Computers) {
	
	#If computer is found, delete, else skip it.
		Try {
		Remove-ADComputer -Filter $Filter 
		
		Start-Sleep -Milliseconds 700
		$Count ++
		}
		Catch {
	       "Computer Not Found: $($Computer.computer)"
		}
	
	
	
	}
	"Count is: $Count"

Open in new window


The result is: Remove-ADComputer : A parameter cannot be found that matches parameter name 'Filter'.

Can someone help me get this working either with scriptblock or via another means? I would rather not modify the input file to contain the "-P" or "-W" suffix.

Thanks in Advance
PowershellActive Directory

Avatar of undefined
Last Comment
AkermanIT
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of AkermanIT
AkermanIT

ASKER

I answered my own question:

I got rid of the scriptblock, and opted for appending "-P" to a variable called $Name before passing it to Remove-ADComputer. In case of error, "-W" will be added to $Name and then that will get passed to Remove-ADComputer to attempt deletion:

$Count = 0

$Computers = Import-CSV "C:\Input.csv" 

ForEach ($Computer in $Computers) {
	
	#If computer-P is found, delete, else try computer-W.
		Try {
			Try {
				$Name = $Computer.computer + "-P"
				"Attempting to remove: $Name"
				Remove-ADComputer $Name -Confirm: $false -ErrorAction Stop
				$Count++		
				}
			Catch {
				$Name = $Computer.computer + "-W"
				"Attempting to remove: $Name"
				Remove-ADComputer $Name -Confirm: $false -ErrorAction Stop
				$Count++		
				}
		}
		Catch {
	       "Computer Not Found: $($Computer.computer)"
		}
	
	
	
	}
	"Count is: $Count"

Open in new window

Avatar of AkermanIT
AkermanIT

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for AkermanIT's comment #a41029101

for the following reason:

I wished I worked this guy. He is awesome. :-)
Avatar of AkermanIT
AkermanIT

ASKER

I like it. Thanks for the help!
Active Directory
Active Directory

Active Directory (AD) is a Microsoft brand for identity-related capabilities. In the on-premises world, Windows Server AD provides a set of identity capabilities and services, and is hugely popular (88% of Fortune 1000 and 95% of enterprises use AD). This topic includes all things Active Directory including DNS, Group Policy, DFS, troubleshooting, ADFS, and all other topics under the Microsoft AD and identity umbrella.

86K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo