PowerShell to copy file

We daily run a Powershell script (v1.0) that checks server health and produce a report in .xls format. The report name is dynamic in nature as it contains the date and time stamp of the execution time (for example, "MorningCheck_<date>_<time>>.xls" i.e., "MorningCheck_16-09-2013@7-30-00.xls").

The report gets automatically stored into the server's local drive - "C:\MorningCheck".

I have also created a mapped drive (Y:) to map a shared network location -
"Y:\Server Health Check\MorningCheckReport"

I want a script with the following features -

It will pick-up the most updated Report (.xls) file from -

It will then create month-wise folder in the network share drive. For example, "Y:\Server Health Chek\MorningCheckReport\September2013"

Copy that most updated Report (.xls file) from "C:\MorningCheck" to the respective month-wise folder -
"Y:\Server Health Check\MorningCheckReport\MorningCheck_<date>_<time>.xls"

I have created following script to meet my above requirements but unable to copy the file into the shared location. Please help me out to resolve this issue.

# Creating Month-wise folder
$SubFolderNameMonth = Get-Date -Format MMMM
$SubFolderNameYear = Get-Date -Format yyyy
$SubFolderNameFull = SubFolderNameMonth + "SubFolderNameYear"

$Location_Y = "Y:\Server Health Chek\MorningCheckReport"

New-Item -Path $Location_Y -Name $SubFolderNameFull -ItemType "directory"

# Select the most updated report
$Report = (GCI C:\MorningCheck | sort LastWriteTime -Descending | Select -First 1).Full

# Copy the updated Report to the shared network path
Copy $Report "Y:\Server Health Chek\MorningCheckReport" + "\" + $$SubFolderNameFull

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can't depend on having a mapped drive available when you run the script under automation.  That is generally tied to an interactive login.

You can either map the drive yourself in the script, or since you are using powershell, just reference the UNC paths directly. --
Instead of using $Location_y = "y:\Server Health Check\...", you will use $Location_y = "\\server\share\server health check"
Your bottom line would be
copy-item $report -destination (join-path -path $Location_y -childpath $SubFolderNameFull)

And I'm lazy, so I'd do the date as:
$SubFolderNameFull = (get-date -format MMMM) + (get-date -format yyyy)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hchabriaAuthor Commented:
Excellent Coralon! Thanks for your valuable inputs and also for your lazy tips. It's working fine now.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.