Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Removing local users with powershell script

$
0
0

I am working on a powershell script intended to delete all local user accounts other than 'Administrator','DefaultAccount,' and 'Guest', deployed by GPO at boot time.

I have something close to what I need, but am getting a type mismatch

here's what I have:

$hostname = $env:computername

$adsiconnect = [adsi]"WinNT://$hostname"

$localusers = @() #I've tried declaring this as an array and not though both were non-functional and I don't think its really necessary

$localUsers = $adsiConnect | ? {(($_.SchemaClassName -eq 'User') -and (-not(($_.path -like '*Administrator') -or ($_.path -like '*Guest') -or ($_.path -like '*defaultAccount'))))}

ForEach ($user in $localUsers)

{

$adsiconnect.delete('User',$user)

}

I know that there is a problem in type mismatch in the second to last line, I have played with a few other ways of doing it and haven't had any luck.

Does anyone have any suggestions?



Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>