[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4372
  • Last Modified:

Powershell remove-aduser in bulk issue

I have this short script taken from a site that allows me to remove in bulk numerous accounts in one shot.

The problem is I am getting this error:

Remove-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and
try the command again.
At C:\userdel\userdel.ps1:2 char:24
+ remove-aduser -identity <<<<  $_.SamAccountName -confirm:$false}
    + CategoryInfo          : InvalidData: (:) [Remove-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.RemoveADU
   ser


The script is here:

Import-Csv .\delete.txt | foreach-object {
remove-aduser -identity $_.SamAccountName -confirm:$false}

The format of delete.txt is:

user1
user2
user3
user4
...

What do I have wrong? It looks like its not picking up the $_.SamAccountName variable but I dont know why?

Thanks!
0
smyers051972
Asked:
smyers051972
1 Solution
 
becraigCommented:
since you say your input is a text file there is no header so .SamAccountName is not needed.

if the file has headers be sure the top row is named:  SamAccountName

Provided the value in the text file is the samaccountname.


or else just use
 gc .\delete.txt | foreach-object {
remove-aduser -identity $_ -confirm:$false}

Open in new window

0
 
smyers051972Author Commented:
This worked! Thanks!
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now