Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does using single quotes with variables yield error "A positional parameter cannot be found that accepts argument"?

I have an variable: $a = sfd. If I use New-Item '1'$a, then it will yield this error:

New-Item: A positional parameter cannot be found that accepts argument 'sdf'.

But if I use New-Item "1$a" then it works. According to How do I concatenate strings and variables in PowerShell? both two methods should work. Do you know why is that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because '1'$a is seen as two separate arguments and the New-Item cmdlet has only 1 positional parameter (-Path) unlike e.g. Write-Host which accepts multiple positional parameters:

$a = sfd
Write-Host '1'$a
1 sfd

(Note that the arguments are separated with a space in the displayed results)

There are several other ways to concatenate strings in PowerShell aside from "1$a", as e.g. New-Item ('1' + $a). For more information see Everything you wanted to know about variable substitution in strings

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading