Solved

help to write a script to copy all folders/subfolders from a network drive location to another share location

Posted on 2013-06-14
16
962 Views
Last Modified: 2013-06-24
Hello Experts

I am beginner in Powershell, and just wondering if you could help me to write a powershell script to do the following in AD

Every single user's profile in AD has a network drive map connection called X. This drive is used by each user to save their documents, folders/files, etc

When someone leaves our company, we would like to copy all content from this X drive[folder/subfolders/files] for each user in AD to be disabled to another secure shared location called LegacyRoot\Users\UserX..

Can anyone please help me with some ideas, best efforts to build an script to accomplish this goal?
0
Comment
Question by:Jerry Seinfield
  • 6
  • 6
  • 3
  • +1
16 Comments
 
LVL 38

Expert Comment

by:Adam Brown
ID: 39248524
All you really need to do is type
copy \\fileservername\sourcefolder\*.* -destination \\server\legacyroot\users\userx  -recurse
and it will do that for you. Is there some kind of automation you're looking for?
0
 
LVL 41

Expert Comment

by:Amit
ID: 39248526
better to use xcopy here.
0
 

Author Comment

by:Jerry Seinfield
ID: 39248649
Thanks for the ideas. I need to build that in powershell, and please keep in mind i have to copy a complete folder structure including but not limited to folders, sub-folders, files from source[my source is a network mapped drive for each user's profile to a another location

Powershell is mandatory, For example

User A has a windows profile, and every time he login into the network, by logon script 3 network drives are automatically mapped. so i have to copy all content from X drive[network mapped drive] to another location

Any ideas?
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39248709
$source = \\uncpath\
$destination = [whateverpath]
copy-item $source $destination -recurse
0
 
LVL 41

Expert Comment

by:Amit
ID: 39248710
are you looking for permission copy too?
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39248856
looks like you want to use move-item vs COPY item
$usernames = get-content -path c:\temp\firedusers.txt
$DestinationRoot = "\\servername\LegacyRoot\User$\"
$uncpath = "\\server1\user$\"
#In your folder redirection policies you will get the ACTUAL path to the redirected users 
# location i.e. \\server1\user$

foreach ($user in $usernames) 
     {
     $source = $uncpath + $user + "\"
     $destination = $DestinationRoot + $user + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     }

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39248862
yes, need to copy all folders and subfolders including permissions

Can you please clarify how can i look into every single user's profile and copy the data from their M drive to destination shared folder?
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39248876
You don't need the users mapped drive.. You have access to the server where these items are stored.

Why even bother with saving permissions for terminated users??? Their account is disabled in AD, I hope!
0
 
LVL 41

Expert Comment

by:Amit
ID: 39248878
that was my doubt, i know what you were looking, basically permission is the main part here. Which is not that easy. Let me check with my friend tomorrow, he wrote something similar long back....
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39248893
$usernames = get-content -path c:\temp\firedusers.txt
$DestinationRoot = "\\servername\LegacyRoot\User$\"
$uncpath = "\\server1\user$\"
#In your folder redirection policies you will get the ACTUAL path to the redirected users 
# location i.e. \\server1\user$

foreach ($user in $usernames) 
     {
     $source = $uncpath + $user + "\"
     $TempAcl = get-acl $source
     $destination = $DestinationRoot + $user + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
     Get-ChildItem $destination -Recurse -Include * -Force | Set-Acl -AclObject $TempAcl -whatif
    #
     }

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39263118
Hi V3eofa,

FilerC is a netapp that hosts all shared folders.

By group policy, and as per attached screen shoot, users will get mapped drive M.

My destination root is another shared folder on a different SAN storage[Dell Compellent]. Both will keep same structure [\\SAN_ShareFolderName\%username%$

I would like to use a CSV file instead of a TXT, please see also csv file attached.

%username%$ represents a variable that indicates user profile.

For example, the user where SAM account name or AdUsername is jdoe has a user profile and if this user is part of the csv file, we must copy the content of M drive to destination root

Based on the CSV file attached,  the group policy and new values for source and destination,

Can i use the code below?

$usernames = get-content -path c:\temp\UserList.csv
$DestinationRoot = "\\DellSAN.domain.on.ca\%username%$"
$uncpath = "\\filerc.domain.on.ca\%username%$"
#In your folder redirection policies you will get the ACTUAL path to the redirected users
# location i.e. \\server1\user$

foreach ($user in $usernames)
     {
     $source = $uncpath + $user + "\"
     $destination = $DestinationRoot + $user + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     }
UserProfileNetworkmappeddrivegro.jpg
UserList3.csv
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39263387
$usernames = import-csv -path c:\temp\UserList.csv
$DestinationRoot = "\\DellSAN.domain.on.ca\"
$uncpath = "\\filerc.domain.on.ca\"
#In your folder redirection policies you will get the ACTUAL path to the redirected users
# location i.e. \\server1\user$

foreach ($user in $usernames)
     {
     $source = $uncpath + $user.ADUsername+ "\"
     $destination = $DestinationRoot + $user.ADUsername + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     } 

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39263492
Hi ve3ofa

You rock,

I will test the script, and keep you posted
0
 

Author Comment

by:Jerry Seinfield
ID: 39264113
Hi Ve3ofa

For the source path in filerc, the user folder is hidden so, Should I  add $ to your path ($source = $uncpath + $user.ADUsername+ "$\")

Another question, I got the final destination path as per below

 \\SAN.domain.on.ca\users$\CIFS_Users$\userAlias

With that said

How would look the final script?
0
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 39264306
$usernames = import-csv -path c:\temp\UserList.csv
$DestinationRoot = "\\SAN.domain.on.ca\users$\CIFS_Users$"
# I think you only need the last $  
$uncpath = "\\filerc.domain.on.ca\"


#In your folder redirection policies you will get the ACTUAL path to the redirected users
# location i.e. \\server1\user$

foreach ($user in $usernames)
     {
     $source = $uncpath + $user.ADUsername+ "\"
#
# here you say  you want it to be useralias but that is not in your csv    
# unless you have a share \\ \\SAN.domain.on.ca\users$\CIFS_Users$\userAlias
# and want to tag the ADUsername after it.
#
$destination = $DestinationRoot + $user.ADUsername + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     }
0
 

Author Closing Comment

by:Jerry Seinfield
ID: 39272955
Many thanks for all the help
0

Join & Write a Comment

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now