• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

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

1 Solution
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)

hchabriaAuthor Commented:
Excellent Coralon! Thanks for your valuable inputs and also for your lazy tips. It's working fine now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now