1
votes

Part of my script looks as follows:

move-item -path $_.FullName+"\*.7z" -destination "$destination"

it returns error:

Move-Item : A positional parameter cannot be found that accepts argument '+*.7z'. At line:32 char:4 + move-item -path $_.FullName+"*.7z" -destination "$destin ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Move-Item], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.MoveItemCommand

But, if I change that variable to exact value, that works:

move-item -path "D:\test1\test2\test3\test4\*.7z" -destination "$destination"

What is the problem?

2

2 Answers

4
votes

To use expressions as part of a command line - which is parsed in argument mode - you must force a new parsing context with (...):

Move-Item -path ($_.FullName+"\*.7z") -destination "$destination"

See Get-Help about_Parsing.


In this instance, given that the expression is constructing a string value, using an expandable (interpolating) string with an embedded subexpression ($(...)) is a viable alternative, as shown in Ben Richard's answer.

0
votes

To do what you want you can use an expression inside a string, without doing the string concatenation.

# $_.FullName is an expression
# so to have used inside the quotes, you need to wrap it with $()
Move-Item "$($_.FullName)\*.7z" $destination