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?
>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