Solved

Powershell - Copy-Item Command

Posted on 2014-03-27
9
1,557 Views
Last Modified: 2014-03-28
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.
0
Comment
Question by:victory2201
  • 5
  • 4
9 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39959924
$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
 

Author Comment

by:victory2201
ID: 39961906
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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39961951
Did you put the paths in ""? For ex "\\server\share 1\folder 1"
0
 

Author Comment

by:victory2201
ID: 39961966
Dan, made some progress. (See attachment)

Its creating the folder and copying the files, however the folder name is incorrect (2045).
EX3.png
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39961972
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
 

Author Comment

by:victory2201
ID: 39961968
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
 

Author Comment

by:victory2201
ID: 39961992
Dan,

That worked like a charm. Thank you very much for assistance with this question. =)
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39961996
Glad I could help!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39962275
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now