1

I'm trying to get PowerShell to evaluate variables before executing a command, for example:

$OutputPath = "C:\Temp"  
Get-ChildItem -include *.mp3 | Move-Item -Destination $OutputPath

However, the Move-Item cmdlet tries to interpret this literally, so it doesn't get moved. The script works fine whenever I enter the path directly, but I need to be able to control the path with a variable. How do I do this?

9
  • 4
    What exact error you got? Commented Jul 22, 2015 at 22:07
  • 3
    The code you posted should do what you expect it to do. Commented Jul 22, 2015 at 22:11
  • 1
    I would also like to know the error. There shouldn't be anything wrong with the code posted. Commented Jul 22, 2015 at 22:11
  • My guess is the use of -include.... that works better with -Recurse. Change it to Get-ChildItem -Filter *.mp3 or just remove the parameter name Get-ChildItem *.mp3. I think that Move-Item is not getting anything from the pipe. Commented Jul 22, 2015 at 23:24
  • 1
    Well the error is explicit. The file is already there. You can use -Force to overwrite (use with caution) and -WhatIf to help with testing. Commented Jul 23, 2015 at 0:44

1 Answer 1

3

This answer could evolve but as your question stands I see a big issue that is unfortunately poorly documented. -Include and -Exclude only perform their intended functions when partnered with -Recurse. Used without it can yeild 0 results. In your case that would mean nothing is passed through the pipe and Move-Item is not executed.

Currently you are just filtering on *.mp3 which is basic and can just be used with the -filter parameter. While you don't need to I would recommended specifying -Path as well so that you move the files you wanted.

Get-ChildItem -Filter *.mp3 | Other-Stuff ...

You mentioned in comments the following error:

Move-Item : Cannot create a file when that file already exists.

That error is very specific. Either from previous testing or an oversight in new names that file indeed already exists. Two things you can do to help with that problem is use the -WhatIf switch which should quote a file path on the verbose stream so you know where the file would end up.

Second, if you understand the data risk, is to use -Force so that the file will be overwritten by the new one. With Copy-Item it is not a big deal since the original file still exists. Mistakes with Move-Item can be permanent.

Sign up to request clarification or add additional context in comments.

3 Comments

Using the WhatIf just returns the script's normal functions, and it finds the media files just fine. However, now, whenever I changed the path, it just creates a generic file with my output folder's name instead of actually moving the files.
@DreadEurosRot You have to show an example of that code in your question and add detail if you can. I can't explain what it is happening with your setup with what I have so far.
@DreadEurosRot, you should create a folder, before move files to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.