none
how to copy a folder and its content on a mapped drive using powershell RRS feed

  • Question

  • Hi all
    i am having problem in copying a recent folder from local to remote pc using net use. Here is my code

         $local = "C:\Projects\Deployments\" ;
         $path = "\\myip\Deployments";
         $recent =  Get-ChildItem -Path $local | Sort-Object -Property LastWriteTime | Select-Object -Last 1
         net use B: $path  /user:username password 
    copy-item -path $recent  -destination "B:\" -recurse -force

        the above script works perfect if i take out $recent line and if i specify exact folder name in $local    
          e.g  $local = "C:\Projects\Deployments\2014-10-13" ;

    i am getting a strange error when i run my script, the directory where i am running this script from it tries to look in that folder the recent folder which needs copying.
    Wednesday, October 15, 2014 9:44 AM

Answers

  • i sorted it out, all i have to do is i write

    copy-item -path $recent.Fullname 

    • Marked as answer by k786 Wednesday, October 15, 2014 11:44 AM
    Wednesday, October 15, 2014 11:44 AM

All replies

  • this script works perfect  

         $local = "C:\Projects\Deployments\2014-10-13" ;
         $path = "\\myip\Deployments";

         net use B: $path  /user:username password 
         copy-item -path $local  -destination "B:\" -recurse -force

    All i would like to do is to copy a recent folder from deployments and paste on the the remote path.

    Wednesday, October 15, 2014 9:48 AM
  • if i do write-host $recent 

    it does shows me that it has stored the latest folder in $recent 

    but as soon as copy command gets executed i dont know why it shows me the location where i am running the script from and tries to look for the recent folder in that location.

    Wednesday, October 15, 2014 10:16 AM
  • Hi mate,

    Have you tried piping the output from the "$recent =  Get-ChildItem -Path $local | Sort-Object -Property LastWriteTime | Select-Object -Last 1" into "copy-item"?  I used the commands below and it seemed to work fine:

    $recent | copy-item -destination "B:" -recurse -force

    Wednesday, October 15, 2014 11:16 AM
  • i sorted it out, all i have to do is i write

    copy-item -path $recent.Fullname 

    • Marked as answer by k786 Wednesday, October 15, 2014 11:44 AM
    Wednesday, October 15, 2014 11:44 AM
  • Try:

    $path = "\\myip\Deployments"
    
    if (!(Test-Path -Path "B:\")) { 
        "Mapping drive 'B:' to $path"
        net use B: $path  /user:username password 
    } else {
        "Drive 'B:' already mapped"
    }
    
    $local = "C:\Projects\Deployments\2014-10-13" 
    $recent =  (Get-ChildItem -Path $local | Sort-Object -Property LastWriteTime | Select-Object -Last 1).FullName
    
    "Copying '$recent' to 'B:\'" 
    Copy-Item -path $recent -destination "B:\" -recurse -force

    $recent was storing the folder name. For this to work, it needs to store the folder's "FullName" which is it's path + name..


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Wednesday, October 15, 2014 11:50 AM
  • It worked like a charm. Thank you very much.

    Brajesh


    Regards, Kumar Brajesh

    Friday, November 21, 2014 7:18 PM
  • It worked for me. Thank you very much
    Tuesday, June 18, 2019 3:36 AM