Answered by:
How to copy a file to a remote server UNC path using Powershell

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
Answers
-
Or...
Copy-Item -Path 'c:\myfolder\myfile' -Destination '\\targetserver\hiddenshare$'
Grant Ward, a.k.a. Bigteddy
- Marked as answer by DBThomson Thursday, March 1, 2012 8:10 PM
All replies
-
Copy-Item -Path 'c:\myfolder\myfile' -Destination '\\targetserver\c$\targetfolder'
Grant Ward, a.k.a. Bigteddy
-
Or...
Copy-Item -Path 'c:\myfolder\myfile' -Destination '\\targetserver\hiddenshare$'
Grant Ward, a.k.a. Bigteddy
- Marked as answer by DBThomson Thursday, March 1, 2012 8:10 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" -
-
-
What doesn't work? What is the error message?
Grant Ward, a.k.a. Bigteddy
-
-
Do you get an error message, or does it just silently not do anything?
Grant Ward, a.k.a. Bigteddy
-
-
-