[powershell] Powershell: A positional parameter cannot be found that accepts argument "xxx"

I am trying to understand what this error actually means. So far a search of similar help requests for this error range from missing parameters, missing pipes, use of single or multi-lines, and also concatenation issues but none of the answers seem to give a definitive reason. So I assume the issue is code format (which makes it a lot harder to track down).

This is my script which I am writing to rename active directory users per target OU from whatever format they are now into a firstname.surname format.

I have created a test OU in AD with some users who will trigger errors and some that will not. However, the users that should not give me an error are giving me the "a positional parameter cannot be found that accepts argument "firstname.surname"

I cannot see what is wrong with the script but hopefully, someone can give me some pointers.

Import-Module ActiveDirectory

$users = $null

$users = Get-ADUser -SearchBase "ou=Testing,ou=Users,dc=my,dc=domain" -Filter * -Properties *
foreach ($user in $users) {
    Write-Host "Processing... $($user)"
    $newname = $null

    # Check first/last name is set
    if (!$user.givenName -or !$user.Surname) {
        Write-Host "$($user) does not have first name or last name set. Please correct, skipping user."
        continue
    } else {
        $newname = ("$($user.givenName).$($user.Surname)")

        #Check if new username already exists
        if (dsquery user -samid $newname) {
            Write-Host "$($user) requires altered username with initial."

            if (!$user.Initials) {
                Write-Host "$($user) does not have any initials set. Please correct, skipping user."
                continue
            }

            $newname = ("$($user.givenName)$($user.Initials).$($user.Surname)")

            #Check if altered new username already exists
            if (dsquery user -samid $newname) {
                Write-Host "$($user) requires manual change. Please correct, skipping user."
                continue
            }
        }

        try {
            #Change UPN
            Set-ADUser $user -userPrincipalName = $newname
            #Change DN
            Rename-ADObject -identity $user -Newname $newname
        } catch {
            Write-Host "Error when renaming $($user). Error is: $($_.Exception.Message). User requires manual change. Please correct, skipping user."
            continue
        }
    }
}

This question is related to powershell active-directory

The answer is


In my case I had tried to make code more readable by putting:

"LONGTEXTSTRING " +
"LONGTEXTSTRING" +
"LONGTEXTSTRING"

Once I changed it to

LONGTEXTSTRING LONGTEXTSTRING LONGTEXTSTRING 

Then it worked


I had to use

powershell.AddCommand("Get-ADPermission");
powershell.AddParameter("Identity", "complete id path with OU in it");

to get past this error


In my case there was a corrupted character in one of the named params ("-StorageAccountName" for cmdlet "Get-AzureStorageKey") which showed as perfectly normal in my editor (SublimeText) but Windows Powershell couldn't parse it.

To get to the bottom of it, I moved the offending lines from the error message into another .ps1 file, ran that, and the error now showed a botched character at the beginning of my "-StorageAccountName" parameter.

Deleting the character (again which looks normal in the actual editor) and re-typing it fixes this issue.


I had this issue after converting my Write-Host cmdlets to Write-Information and I was missing quotes and parens around the parameters. The cmdlet signatures are evidently not the same.

Write-Host this is a good idea $here
Write-Information this is a good idea $here <=BAD

This is the cmdlet signature that corrected after spending 20-30 minutes digging down the function stack...

Write-Information ("this is a good idea $here") <=GOOD


I had a similar challenge when writing a Powershell script to interact with AWS CLI using the AWS Powershell Tools

I ran the command:

Get-S3Bucket // List AWS S3 buckets

And then I got the error:

Get-S3Bucket : A positional parameter cannot be found that accepts argument list

Here's how I fixed it:

Get-S3Bucket does not accept // List AWS S3 buckets as an attribute.

I had put it there as a comment, but it's not acceptable by the AWS CLI as a comment. AWS CLI rather sees it as a parameter.

I had to do it this way:

#List AWS S3 buckets
Get-S3Bucket

That's all.

I hope this helps


I had this problem when trying to change directory, using the character _. The solution was to use a string to change directories.

C:\> cd "my_new_dir"

In my case it was the distinction between (En dash) and -(Hyphen) as in:

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

and:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

Dashes, Hyphens, and Minus signs oh my!