Get-ChildItem results looks like relative paths in Powershell

Question:

I would like to scan and move folders (and sub folders or even deeper) from one folder to another using Powershell.

Currently I’m using this pipe of commands.

Unfortunately it doesn’t work because the found results are relative to .\sub\WORK, when trying to move them Move-Item complains that the folders are not in the current folder:

I expect that $_ would contain: 'C:\TMP\sub\WORK\2011-12-12 test 2 OK' because these are objects in Powershell and no strings like in Linux.

Answer:

In case you use Get-ChildItem, be very careful. The best way is to pipe the objects to Move-Item and you don’t need to think about it more:

(no need to use Foreach-Object)

The main reason why I’m answering is this one: Get-ChildItem constructs object differently, depending on the parameters. Look at examples:

Then if you use $_ in a cycle and PowerShell needs to convert FileInfo from Get-ChildItem to string, it gives different results. That happened when you used $_ as argument for Move-Item. Quite bad.

I think there is a bug that reports this behaviour.

Source:

Get-ChildItem results looks like relative paths in Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply