0

I have a list of files which I would like to move to other destinations. Issues:

  1. Destination is different
  2. Errors can occur, e.g. File does already exist in Destination.

If so, I would like to delete the existing file (e.g. $StartM\C\A.Lnk)

How can I do this with a list / foreach or other possibility?

Move-Item -Path "$StartM\C\A.Lnk" -Destination "$StartM\Maintenance"
Move-Item -Path "$StartM\C\B.Lnk" -Destination "$StartM"

2 Answers 2

1

The canonical way of dealing with a situation like that is to define a mapping between sources and destinations:

$shortcuts = @{
    "$StartM\C\A.Lnk" = "$StartM\Maintenance"
    "$StartM\C\B.Lnk" = "$StartM"
}

and then process that mapping like this:

foreach ($src in $shortcuts.Keys) {
    Remove-Item $shortcuts[$src] -Force -EA SiltenlyContinue
    Move-Item $src -Destination $shortcuts[$src]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Ansgar. Please see my answer and issues below: stackoverflow.com/a/59007839/12403339
0

Thank you Ansgar. I got some issues:

1. Remove-Item: The Argument can not be bound with the Parameter "Path", because it is an empty string.

  • CategoryInfo: InvalidData: (:) [Remove-Item], ParameterBindingValidationException
  • FullyQualifiedErrorId: ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand

2. Move-Item: No position parameter was found, which accept the argument "\Maintenance"

May be there is a missunderstanding. Source should be deleted if exists in Destination. E.g.: "$StartM\C\A.Lnk" = "$StartM\Maintenance". Goal: "$StartM\C\A.Lnk" should be deleted if "A.Lnk" exists in "$StartM\Maintenance\A.Lnk".

  • Move-Item $src = $shortcuts[$src]
  • CategoryInfo: InvalidArgument: (:) [Move-Item], ParameterBindingException
  • FullyQualifiedErrorId: PositionalParameterNotFound,Microsoft.PowerShell.Commands.MoveItemCommand

3. ErrorAction Remove-Item: The Parameter "ErrorAction" can not be bound. Type "SiltenlyContinue" can not be converted in type "System.Management.Automation.ActionPreference" ... I just changed "-EA" ... -Force -EA SilentlyContinuefrom to "-Error Action". This type of Error has gone.

4. Double keys If there are some constelations in the array like this: "$StartM\C\B.Lnk" & "$End\C\B.Lnk" I got an error that double keys are not allowed in Hashliterals.

  • CategoryInfo : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException
  • FullyQualifiedErrorId : DuplicateKeyInHashLiteral

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.