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

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?
Jerry SeinfieldAsked:
Who is Participating?
 
David Johnson, CD, MVPConnect With a Mentor OwnerCommented:
$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
 
Adam BrownSr Solutions ArchitectCommented:
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
 
AmitIT ArchitectCommented:
better to use xcopy here.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Jerry SeinfieldAuthor Commented:
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
 
David Johnson, CD, MVPOwnerCommented:
$source = \\uncpath\
$destination = [whateverpath]
copy-item $source $destination -recurse
0
 
AmitIT ArchitectCommented:
are you looking for permission copy too?
0
 
David Johnson, CD, MVPOwnerCommented:
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
 
Jerry SeinfieldAuthor Commented:
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
 
David Johnson, CD, MVPOwnerCommented:
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
 
AmitIT ArchitectCommented:
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
 
David Johnson, CD, MVPOwnerCommented:
$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
 
Jerry SeinfieldAuthor Commented:
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
 
David Johnson, CD, MVPOwnerCommented:
$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
 
Jerry SeinfieldAuthor Commented:
Hi ve3ofa

You rock,

I will test the script, and keep you posted
0
 
Jerry SeinfieldAuthor Commented:
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
 
Jerry SeinfieldAuthor Commented:
Many thanks for all the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.