Author Posts

January 29, 2017 at 1:13 am

Folders.csv contains the names of three folders, test001, test002 and test003. I want to move test001 and test002 from the folder \\server\share\MoveFrom to the folder \\server\share\MoveTo.
I can import the list but when I try to move only the folders in the list, the whole parent folder \\server\share\MoveFrom gets moved into the destination \\server\share\MoveTo.
I am using Powershell v2.

$Foldernames = Import-Csv "Folders.csv"
#I've also tried Get-Content "Folders.txt" without a header but I still get the same result
$Sourcepath = \\server\share\MoveFrom
$Destpath = \\server\share\MoveTo
$Foldernames | foreach { Move-Item -path $sourcepath -dest $Destpath }

Any help is appreciated!

January 29, 2017 at 3:47 am

If you use the pipeline you should use the pipeline variable as well. I assumed that the header of your csv file is "Path"

$Sourcepath = \\server\share\MoveFrom
$Destpath = \\server\share\MoveTo
$Foldernames | ForEach-Object {
    $Source = Join-Path -Path $sourcepath -ChildPath $_.Path
    $Destination = Join-Path -Path $Destpath -ChildPath $_.Path
    Move-Item -Path $Source -Destination $Destination 
}

... or you could use a foreach loop like this:

$Foldernames = Import-Csv "Folders.csv"
$Sourcepath = \\server\share\MoveFrom
$Destpath = \\server\share\MoveTo
ForEach($FolderName in $Foldernames) {
    $Source = Join-Path -Path $sourcepath -ChildPath $FolderName
    $Destination = Join-Path -Path $Destpath -ChildPath $FolderName
    Move-Item -Path $Source -Destination $Destination 
}

I am using Powershell v2.

Why?? Don't do that! Upgrade! 😉 😀

January 29, 2017 at 5:04 am

Thank you for your reply...I am still seeing some wonky behavior. The parent \\server\share\MoveFrom folder gets moved into the destination folder....I'm starting to think maybe the join-path isn't working correctly. "$Source" appears to be the parent \\server\share\MoveFrom folder instead of one of the subfolders that I want moved. I even tried using v4, same result. I do appreciate your input.

January 29, 2017 at 10:57 am

OK – Sorry. I shouldn't answer forum posts when I actually should go to bed. 😉 Now I'm awake.

If you just like to move all subfolders from your $SourcePath you could do this:

$Sourcepath = \\server\share\MoveFrom
$Destpath = \\server\share\MoveTo
Get-ChildItem -Path "$Sourcepath" -Directory | ForEach-Object { Move-Item -Path $_.FullName -Destination "$Destpath"}

For a PS version before 3 it should be this:

$Sourcepath = \\server\share\MoveFrom
$Destpath = \\server\share\MoveTo
Get-ChildItem -Path "$Source" | Where-Object {$_.PSIsContainer} | ForEach-Object { Move-Item -Path $_.FullName -Destination "$Destination"}

unfortunately the parameters -Directory and -File didn't exist before.

And if you want to move just the folders from your list and leave the rest – this should do the trick.

$Sourcepath = \\server\share\MoveFrom
$Destpath = \\server\share\MoveTo
$Foldernames = Import-Csv "Folders.csv"
Get-ChildItem -Path "$Sourcepath" -Directory | Where-Object {$FolderNames.Path -contains $_.BaseName} |  ForEach-Object { Move-Item -Path $_.FullName -Destination "$Destpath"}

(Again: I assumed that the header in your csv file is "Path".)

BTW: If those folders are quite 'heavy' you should think about to use robocopy. It is made for and way faster than plain Powershell.

January 29, 2017 at 3:16 pm

The Where-Object was the key! This works perfectly. Thank you and I hope you got some sleep!

January 29, 2017 at 4:03 pm

Great. Glad it was helpful. 🙂