21

I am trying to get the meta data from a directory and I am getting an error that A positional parameter cannot be found that accepts argument '\'. Not sure how to correct this?

$FileMetadata = Get-FileMetaData -folder (Get-childitem $Folder1 + "\" + $System.Name + "\Test" -Recurse -Directory).FullName
user1342164
  • 1,434
  • 13
  • 44
  • 83
  • I get same error Get-ChildItem : A positional parameter cannot be found that accepts argument '\\'. – user1342164 Aug 07 '14 at 12:22
  • Possible duplicate of [Positional Parameter error in powershell script](https://stackoverflow.com/questions/39407004/positional-parameter-error-in-powershell-script) – Jim G. May 17 '18 at 16:15
  • If you are getting this when trying to disable directory colors, perhaps see https://stackoverflow.com/a/59065276 – tripleee Aug 24 '22 at 10:05

4 Answers4

20

You need to do the concatenation in a subexpression:

$FileMetadata = Get-FileMetaData -folder (Get-childitem ($Folder1 + "\" + $System.Name + "\Test") -Recurse -Directory).FullName

or embed the variables in a string like this:

$FileMetadata = Get-FileMetaData -folder (Get-childitem "$Folder1\$($System.Name)\Test" -Recurse -Directory).FullName
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
3

The most robust way in Powershell to build a path when parts of the path are stored in variables is to use the cmdlet Join-Path.

This also eliminate the need to use "\".

So in your case, it would be :

$FoldersPath = Join-Path -Path $Folder1 -ChildPath "$System.Name\Test"

$FileMetadata = Get-FileMetaData -folder (Get-ChildItem $FoldersPath -Recurse -Directory).FullName
Mathieu Buisson
  • 1,325
  • 10
  • 8
  • If you want a property of an object variable expanded inside a string you need to put it in a subexpression: `"$($System.Name)\Test"`. However, if you're using `Join-Path` anyway (which is a good idea), you should go all the way: `$SystemFolder = Join-Path -Path $Folder1 -ChildPath $System.Name; $TestFolder = JoinPath -Path $SystemFolder -ChildPath 'Test'`. – Ansgar Wiechers Aug 07 '14 at 13:04
  • 3
    the "most robust way" to build paths in Powershell should be to have JUST ONE WAY TO DO THINGS, NOT 1 BILLION DIFFERENT METHODS. Microsoft, please learn by Linux and *nix in general how to do stuff consistently – Gianluca Ghettini Mar 21 '17 at 10:09
1

If you come from the world of VBScript. With Powershell, every space is interpreted as completely separate parameter being passed to the cmdlet. You need to either place the formula in parentheses to have the formula evaluated prior to passing it as the path parameter or enclosing with quotes also works:

Wont work, Powershell thinks this is two parameters:

$Folder1 + "\" + $System.Name

Will work with brackets:

($Folder1 + "\" + $System.Name)

Will also work together when enclosed in quotes:

"$Folder1\$System.Name"

Ref.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

I was trying to run the sc create <name> binPath=... command and kept getting the positional character issue. I resolved this by running the command in Command Prompt not PowerShell. Ugh.

Sators
  • 2,746
  • 1
  • 20
  • 26