Powershell error positional parameter cannot be found that accepts argument

Dei Bertine 21 Reputation points
2021-02-10T01:50:54.833+00:00

Greetings,
I'm trying to create a simple ps script that will clear telephone number from a large user group in AD, pulling from a csv file however keep getting this error and not sure where or why it is failing...I'd appreciate the feedback or any assistance you could provide. Cheers.

$users = Import-Csv -Path C:\user.csv
foreach ($user in $users) {
Get-ADUser $user.SamAccountName -Properties * -SearchBase 'ou=Users,DC=cotm,DC=local' | Set-ADUser -Clear telephone
}

ERROR:

Get-ADUser : A positional parameter cannot be found that accepts argument 'fletterman'.
At C:\phone.ps1:5 char:1

  • Get-ADUser $user.SamAccountName -Properties * -SearchBase 'ou=...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
    Get-ADUser : A positional parameter cannot be found that accepts argument 'gramone'.
    At C:\phone.ps1:5 char:1
  • Get-ADUser $user.SamAccountName -Properties * -SearchBase 'ou=...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-02-10T03:26:33.03+00:00

    Hi,

    You need to use the -filter parameter to specify an ADUser property and the attribute should be "telephonenumber", not "telephone"

    foreach ($user in $users) {  
        $sam = $user.SamAccountName  
        Get-ADUser -Filter {SamAccountName -eq $sam} -Properties * -SearchBase 'ou=Users,DC=cotm,DC=local' | Set-ADUser -Clear telephonenumber  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-02-10T03:19:14.147+00:00

    Because you used the -SearchBase parameter you cannot use the -Identity parameter. The -Identity parameter accepts a unique identifier -- there's no search for a match as there is with a -Filter or -LDAPFilter, so you can't specify where in the AD to look. The error is the result of there being three different parameter sets for the Get-ADUser cmdlet and you've used a parameter that excludes the use of the -Identity parameter.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.