Powershell script to remove local users from computer

I have a function that works great from powershell to remove users. I can type remove-user and am prompted for system name, and then username. so it works great, and removes the user. I'd like to be able to use another script to use the function to remove several accounts from many servers. So I wrote this:

import-module .\functions.ps1
$computer = get-content servers.txt
$user      = get-content users.txt
Remove-LocalUser $computer $user

When I run it (from the same powershell session that works just fine 'manually'.
When I use the above script,  It posts "user not found".
If I check the system the user is there, and if I then use the function manually, it deletes the account.
I am sure I need to add more to my script, what do I need?
LVL 3
SquigglyMonkeyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

oBdACommented:
Try it like this:
import-module .\functions.ps1
ForEach ($Computer In (Get-Content servers.txt)) {
	ForEach ($User In (Get-Content users.txt)) {
		Remove-LocalUser $computer $user
	}
}

Open in new window

SquigglyMonkeyAuthor Commented:
Thank you , that is almost working. I am guessing that the way I am trying to do this, and the function don't get completely along.
I ran it as you have, with 1 server in the servers text, and 2 names in the users text, and it returns:
user not found
Deleted

When I look at the users in the server, I can see that both are now gone. weird....
oBdACommented:
Could it be that the first user was already deleted by one of your former attempts?
This version lets you know which user on which machine it's currently processing:
import-module .\functions.ps1
ForEach ($Computer In (Get-Content servers.txt)) {
	"Working on '$($Computer)':" | Write-Host
	ForEach ($User In (Get-Content users.txt)) {
		"`t- $($User)" | Write-Host
		Remove-LocalUser $computer $user
	}
}

Open in new window

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
SquigglyMonkeyAuthor Commented:
This mostly works, although it still shows one user as not found and one as deleted. I checked many of the servers before the running the script, and they had userA and userB, the script showed that userA was 'user not found' and userB '  as 'deleted'. Then I checked the systems afterwards both accounts were gone, Weird...
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.