locked
Scripting and joining variable with string RRS feed

  • Question

  • I need to craft a script which will copy all content from one user directory to another using a list of user names in a text file.  The tricky part for me is that the source and target directories are the same but different - the source names have the text "usr" at the end.  I tried writing a script which appended "usr" to the end, and also a script which references two separate text files (one of which had the names with usr at the end already), but neither worked properly.

    In essence what I need to do is for each user in a list, "copy-item \\mvmfsr02\home\DTESTERusr\* "\\MFSR04\USERS\DTESTER" -recurse -force."  Here's one attempt I made at scripting this.  This example is where I tried using two different lists.

    $user = Get-Content \\mvmfsr02\home\mkgusr\usrtestUSERLIST.txt
    (example content was DTESTERusr, PTESTERusr)
    
    
    
    Get-Content \\mvmfsr02\home\mkgusr\testUSERLIST.txt
    (example content DTESTER, PTESTER)
    
    | ForEach-Object {
    
    
    
    copy-item \\mvmfsr02\home\$user\*
    
    "\\MFSR04\USERS\DTESTER"
    
    -recurse -force
    
    
    
    }


    Monday, January 22, 2018 3:21 PM

Answers

  • You could start with something like this, but there is of course a lot of room for improvements:

    Get-Content -Path '\\mvmfsr02\home\mkgusr\usrtestUSERLIST.txt' |
        ForEach-Object {
            Copy-Item -Path "\\mvmfsr02\home\$_" -Destination "\\MFSR04\USERS\$($_ -replace 'usr$','' )" -Recurse -Force
        }

    (untested!)

    Please take a little time to learn the basics of Powershell.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, January 22, 2018 3:46 PM
    • Marked as answer by Anon-a-Mouse Monday, January 22, 2018 9:47 PM
    Monday, January 22, 2018 3:45 PM

All replies

  • You could start with something like this, but there is of course a lot of room for improvements:

    Get-Content -Path '\\mvmfsr02\home\mkgusr\usrtestUSERLIST.txt' |
        ForEach-Object {
            Copy-Item -Path "\\mvmfsr02\home\$_" -Destination "\\MFSR04\USERS\$($_ -replace 'usr$','' )" -Recurse -Force
        }

    (untested!)

    Please take a little time to learn the basics of Powershell.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, January 22, 2018 3:46 PM
    • Marked as answer by Anon-a-Mouse Monday, January 22, 2018 9:47 PM
    Monday, January 22, 2018 3:45 PM
  • Yes!!  I only had to make one tweak - I added \* after ....home\$_.  Thank you so much!

    Get-Content -Path '\\mvmfsr02\home\mkgusr\usrtestUSERLIST.txt' |
        ForEach-Object {
            Copy-Item -Path "\\mvmfsr02\home\$_\*" -Destination "\\MFSR04\USERS\$($_ -replace 'usr$','' )" -Recurse -Force
        }

    Monday, January 22, 2018 9:47 PM