Powershell - Copy-Item Command

Copy-Item -path c:\folder –destination \\server\share

Basic PS command, does exactly what it should. However the files that are being copied over daily are config files for a database we use on one of our Linux boxes. (This is presented through a Samba share)

However, I need it to copy files over nightly, but place them in subfolders based on the date they were copied.

Example:

c:/share/3272014
c:/share/3282014
etc.

Any suggestions on how to accomplish this would be most appreciated. Thanks.
victory2201Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
OK, just tested and this works:

$date=(Get-Date).ToString("Mdyyyy")
Copy-Item -path c:\folder –destination \\server\share\$date -Recurse

Open in new window

0
 
Dan CraciunIT ConsultantCommented:
$date = Get-Date
$folder = $date.month + $date.day + $date.year
Copy-Item -path c:\folder –destination \\server\share\$folder

Open in new window

or
Copy-Item -path c:\folder –destination \\server\share\(Get-Date).ToString("Mdyyyy")

Open in new window

HTH,
Dan
0
 
victory2201Author Commented:
Dan,

Thank you for the assistance.

When I ran the top option, it created a file in the corresponding directory but not a folder containing all the files. (first attachment)

The second option just threw the error provided in the second attachment.
EX1.png
EX2.png
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
Dan CraciunIT ConsultantCommented:
Did you put the paths in ""? For ex "\\server\share 1\folder 1"
0
 
victory2201Author Commented:
Dan, made some progress. (See attachment)

Its creating the folder and copying the files, however the folder name is incorrect (2045).
EX3.png
0
 
victory2201Author Commented:
No, I didn't put "" in the path.

Is that required? I don't recall having to do that unless there are spaces in the folder path.
0
 
victory2201Author Commented:
Dan,

That worked like a charm. Thank you very much for assistance with this question. =)
0
 
Dan CraciunIT ConsultantCommented:
Glad I could help!
0
 
Dan CraciunIT ConsultantCommented:
BTW, if you want a one-liner, this is a working version:
Copy-Item -path c:\folder –destination \\server\share\$((Get-Date).ToString("Mdyyyy")) -Recurse

Open in new window

0
All Courses

From novice to tech pro — start learning today.