Jason
Jason

Reputation: 1

Moving folders using PowerShell and CSV file

We have identified via another PS script a shed load of user directories that are not active any more and wish to MOVE these user folders from the existing to an archive location.

I've got a far as identifying the users and have them outputted to a .csv file, e.g. the content is similar to below:

E:\Shares\Users$\AbassR
E:\Shares\Users$\AbbottJ
... and so on

I would like to use PowerShell and robocopy to avoid the long file name restriction and any other limitations Windows OS has.

I did find this script that I've modified:

GC -Path G:\Shares\ArchiveUsers$\userstomove.csv | ForEach-Object {
  robocopy $_ G:\Shares\ArchiveUsers$\Watford /E /Z /R:1 /W:1 /LOG+:G:\userscopied.txt /NP
}

It does copy (not move), but also it does not recreate the original folders in the destination. It seems to only copy files from the folders shown in the .csv file and copies to root of the destination archive folder.

It would make my life so much easier if it could MOVE (perhaps with the robocopy /mov switch) the source folders identified in the .csv file to a folder with the same name in the destination archive location) alongside with all the other folders we had previously manually migrated.

Upvotes: 0

Views: 1379

Answers (2)

Jason
Jason

Reputation: 1

In the end this is the script that I ended up with, did the job. However I did have subsequent problems of moving sub folders of users directories such as My Pictures that I wasn't the owner of.

GC -Path G:\Shares\ArchiveUsers$\userstomove.csv | ForEach-Object {
    $dst = 'G:\Shares\ArchiveUsers$\Watford'
    $name = [IO.Path]::GetFileName($_)
    robocopy $_ "$dst\$name" /move /e /z /r:1 /w:1 /TEE /log+:G:\Shares\ArchiveUsers$\userscopied.txt /np
   }

Thanks for all the input.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Using the parameter /e already makes robocopy copy directories recursively. However, robocopy copies the content of the source directories to the destination directory, not the entire directory, thus effectively merging/overwriting files from different users.

To have the source directories copied to individual destination directories you need to specify those individual directories as the destination. To move the source directories to the destination replace /e with /move.

$dst = 'G:\Shares\ArchiveUsers$\Watford'

... | ForEach-Object {
    $name = [IO.Path]::GetFileName($_)
    robocopy $_ "$dst\$name" /move /z /r:1 /w:1 /log+:G:\userscopied.txt /np
}

Upvotes: 2

Related Questions