Powershell formatting command

Posted on 2013-02-07
Last Modified: 2013-02-11
# QCM_Archive.ps1

# Load Template filename template
$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";

# Load Days variable
# Set to -1 to take yesterdays date.
$days = -1

# Calculate PastDay variable using $days value
$pastdate = [datetime]::Now.AddDays($days)
#Write-Host "$pastdate"

# Specify where to read the files from
# A share locally on current server
$FolderPath = Get-ChildItem "V:\Capacity Manager Size reports\"

# Loop 1 

# Load $Files variable 
Foreach ($Files in $FolderPath)

# Loop 2
# Load template variable with formatted filenames
# IF Loop
# Use Like to load variable files using template to determine the correct day to load
# Like uses template filename at start of file and then '*' used to ignore the end of the file name which is variable
# Copy-Item cmdlet 
# Where to copy the calculated $files value 

    {foreach($template in $Report_Template)
        {If ($files.Name -like $($template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year +"*.pdf"))
             {Copy-Item -Path $files.FullName "\\Db-occ14\CapacityManagerReports_Archive\"}

Open in new window

Please can someone take a look at the code and change it to use format command instead of the current method.

The script had been working when created in Oct 2012 - the script was run once at the end of each month.

But in the new year has stopped working because the $pastdate.month variable is holding value of '01' and the code cant handle the padding with the 01

Can someone help with a way to use powershell formatting to avoid the padding issue ?

Question by:mooriginal
  • 5
  • 3
LVL 69

Expert Comment

ID: 38863893
$pastdate.month will certainly not be '01' - it is 2 (integer). I assume your issue is that you NEED padding, but that should then be the case with the day from start. Can you clarify the expected filename format?

Author Comment

ID: 38867588
Im not sure of the format to be clear.

At present because the datetime is bringing back date formate of 01
the script is not working as expected.

I need a way to handle the 01, 02, etc for months until we get into a double digit month like Oct 10.

I thought the best way to do this would be ask for a way of avoiding the current method using formatting or working around it ...

Also we use the UK format for date here - so DD/MM/YY

For example a file format that is correct and should be copied is following:

The original script was working when the format of the file being checked was like this
LVL 69

Accepted Solution

Qlemo earned 500 total points
ID: 38867886
Ah, I see, the day is always 2 digits as we are executing the script at the end of month anyway, so this "failure" is not relevant.
There are many methods to properly format the date. To stay with what you already have, change line 33 to:
        {If ($files.Name -like $($template + $pastdate.Day + "_" + ([String]$pastdate.Month).PadLeft(2,'0') + "_" + $pastdate.Year +"*.pdf"))

Open in new window

Another way is, using .NET formatting:
        {If ($files.Name -like "$template {0:dd_MM_yyyy}*.pdf" -f $pastdate

Open in new window

which looks much shorter and is more straight-forward.
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Author Comment

ID: 38868139
This looks a great answer - unfortunately both code snippets didnt work - or at least they ran but didnt copy any files...

The folder in question that is being read currently has files from 28th Jan - to 8th Feb.

So tried to run both changes - expecting to copy files from yesterdays date - 7th feb.

Nada so far ...

Also when I switched on the this  

$pastdate = [datetime]::Now.AddDays($pastdays)
Write-Host "$pastdate"
Result : 02/07/2013 15:13:53

Dont know if that helps
LVL 69

Expert Comment

ID: 38868204
The second code snippet expects an space before the date, which is "wrong"
        {If ($files.Name -like "$template{0:dd_MM_yyyy}*.pdf" -f $pastdate

Open in new window

However, the first one should have worked as-is, if your template is correct. The template needs to be
or else we have to change the like pattern.

Author Comment

ID: 38872883
ill give these a try on monday - the template has not changed as far as i know and as i said this process was working before 2013 ....

Author Comment

ID: 38875196
Your 2nd script also was missing a closing ')'
However it still did not work.

I checked the template - and there was 1 servername error in it - which is strange because it still worked in 2012 but just ignored this particular file in the copy.

Nevertheless after tidying up template - and running your
        {If ($files.Name -like $($template + $pastdate.Day + "_" + ([String]$pastdate.Month).PadLeft(2,'0') + "_" + $pastdate.Year +"*.pdf"))

Worked a treat


Author Closing Comment

ID: 38875197
Well explained and thought through

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Learn about cloud computing and its benefits for small business owners.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

13 Experts available now in Live!

Get 1:1 Help Now