Solved

Powershell - Copy-Item Command

Posted on 2014-03-27
9
1,547 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
Comment Utility
$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
Comment Utility
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
Comment Utility
Did you put the paths in ""? For ex "\\server\share 1\folder 1"
0
 

Author Comment

by:victory2201
Comment Utility
Dan, made some progress. (See attachment)

Its creating the folder and copying the files, however the folder name is incorrect (2045).
EX3.png
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Dan,

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

Expert Comment

by:Dan Craciun
Comment Utility
Glad I could help!
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
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…
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now