Moving Inactive Home Folders

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 3 months, 3 weeks ago.

  • Author
    Posts
  • #62925
    Profile photo of Kim Vogel
    Kim Vogel
    Participant

    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!

  • #62928
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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! 😉 😀

  • #62931
    Profile photo of Kim Vogel
    Kim Vogel
    Participant

    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.

  • #62938
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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.

  • #62941
    Profile photo of Kim Vogel
    Kim Vogel
    Participant

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

    • #62950
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Great. Glad it was helpful. 🙂

You must be logged in to reply to this topic.