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
LVL 1
AkermanITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Probably best to search for an existing computer object that matches the name, and then pass that on to Remove-ADComputer.  The following assumes an input .CSV file with a "computer" header.
Import-CSV "C:\Input.csv" | ForEach `
{
    $Computer = $_.computer
	Get-ADComputer -Filter {name -like "$Computer-*"} | Remove-ADComputer
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AkermanITAuthor Commented:
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

0
AkermanITAuthor Commented:
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. :-)
0
AkermanITAuthor Commented:
I like it. Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.