none
How to copy a file to a remote server UNC path using Powershell RRS feed

  • Question

  • I am trying to copy file(s) from a Windows 2003 server running SP2 to another Windows 2003 R2 SP2 server's hidden share.

    How would I do that in powershell?

    example:

    copy c:\temp\test.txt to \\servername\sharename$

    Thanks!



    • Edited by DBThomson Thursday, March 1, 2012 7:03 PM
    Thursday, March 1, 2012 6:51 PM

Answers

All replies

  • Copy-Item -Path 'c:\myfolder\myfile' -Destination '\\targetserver\c$\targetfolder'


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, March 1, 2012 7:03 PM
  • Or...

    Copy-Item -Path 'c:\myfolder\myfile' -Destination '\\targetserver\hiddenshare$'


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by DBThomson Thursday, March 1, 2012 8:10 PM
    Thursday, March 1, 2012 7:05 PM
  • This is what I have and it runs fine on my Windows 7 computer but not on my Windows 2003 server.

    # Here I define the folders and the extensions to look at.
    $folder1 = ls "F:\Folder1\Inbound" -recurse -include "*.pdf"
    $folder2 = ls "F:\Folder2\Inbound" -recurse -include "*.txt"
    $destinationfolder = "\\servername\import$"


    # I set the variables for the amount of files and folders so I can use that as a parameter for the two
    # loops which I use to find duplicate files. 
    $folder1count = $folder1.count 
    $folder2count = $folder2.count 


    # These are the main two loops, what it does is compare folder1\file1 to all files in folder2. If it finds no match it
    # moves on to compare folder1\file2 to all files in folder2 etc.etc.
    for ($j=0;$j -lt $folder1count;$j++) { 
        for ($k=0;$k -lt $folder2count;$k++) { 


    # The compare object statement compares the basename, file name without extension or path of the files
    # in folder1 and folder2. If a match is found it moves both files to $destination folder
            if (compare-object $folder1[$j].basename $folder2[$k].basename -excludedifferent -includeequal) { 


    # Here are the commands to move the file, note how I use .fullname now instead of .basename
    # -force is to force it to move the file.
                move-item $folder1[$j].fullname $destinationfolder -force 
                move-item $folder2[$k].fullname $destinationfolder -force 


    # Copy count counts the number of files copied
                $copycount = $copycount + 2 
            } 
        } 



    # This is the friendly message informing you how many files were copied 
    write-host "$copycount Files moved" 

    Thursday, March 1, 2012 7:08 PM
  • Hi,

    When questions about copying files come up, my knee-jerk reaction is to recommend robocopy.

    Bill

    Thursday, March 1, 2012 7:12 PM
    Moderator
  • I have used robocopy but I am trying to complete the entire task in one script which is using powershell.
    Thursday, March 1, 2012 7:13 PM
  • What doesn't work?  What is the error message?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, March 1, 2012 7:20 PM
  • The files do not move to the $destinationfolder when running on a Windows 2003 SP2 server but they do on my Windows 7 computer.
    Thursday, March 1, 2012 7:24 PM
  • Do you get an error message, or does it just silently not do anything?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, March 1, 2012 7:26 PM
  • No error message and silently nothing.
    Thursday, March 1, 2012 7:37 PM
  • Hi,

    You could call robocopy from the PowerShell script.

    Bill

    Thursday, March 1, 2012 8:03 PM
    Moderator
  • I got it to work.

    I removed and re-added the ls in the folder1 and folder 2 and that seem to fix it.

    There may have been corruption in the file.

    Thanks all!

    Thursday, March 1, 2012 8:11 PM