Why doesn't my Powershell script work?

I have a Powershell script that seems to run ok, no errors and taxes the CPU, but does not do anything. The server will run this Monday Thru Friday at 7 pm.  The purpose of this is to find all pdf files that was created yesterday and move them to a new directory named by the LastWriteTime.Date.  When our ERP system does its end of the day, it generates all the assembly releases, in pdf format,  for the next day to be built. These are created in the directory named 'release_only'. We like to keep this directory clean so it doesn't get cluttered and eventually slow down another script that we run from the same location.

$src = '\\svr1\assembly_dept\schedule\release_only'

$dst = '\\svr1\assembly_dept\shedule\release_archive'

$file = Get-ChildItem -Path $src -Include '*.pdf' | Where{$_.LastWriteTime -eq (Get-Date).AddDays(-1)}

if ($file = (Get-ChildItem -Path $src -Include '*.pdf' | Where{$_.LastWriteTime -eq (Get-Date).AddDays(-1)}))
{
   $dstdir = $dst + '\' + $file.LastWriteTime.Date.ToString('yyyy') + '\' + $file.LastWriteTime.Date.ToString('MM - MMM') + '\' + $file.LastWriteTime.Date.ToString('MMM. dd')
 
   if(!(test-path $dstdir))
     {
      New-Item -ItemType Directory -Force -Path $dstdir
     }
   Move-Item -Path $src -Destination  $dstdir

Open in new window


I could use a second set of eyes to help me figure this one out. Would really appreciate some help.
LVL 1
Steve WilliamsProduct Design EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

NorieAnalyst Assistant Commented:
Steve

Could it be that LastTImeWrite includes a time portion so the Where clause here isn't finding matches?
$file = Get-ChildItem -Path $src -Include '*.pdf' | Where{$_.LastWriteTime -eq (Get-Date).AddDays(-1)}

Open in new window

Perhaps you should use .LastWriteTime.Date to get the date only.
Steve WilliamsProduct Design EngineerAuthor Commented:
I did as you suggested and that didn't help. I then changed these to lines of code to include the date format. And that didn't help any either.

$file = Get-ChildItem -Path $src -Include '*.pdf' | Where{$_.LastWriteTime.Date.ToString('yyyyMMdd') -eq (Get-Date).AddDays(-1).ToString('yyyyMMdd') }

if ($file = (Get-ChildItem -Path $src -Include '*.pdf' | Where{$_.LastWriteTime.Date.ToString('yyyyMMdd') -eq (Get-Date).AddDays(-1).ToString('yyyyMMdd') }))

Open in new window

NorieAnalyst Assistant Commented:
Steve

If you run this do you see a list of the PDFs you want to move?
$src = '\\svr1\assembly_dept\schedule\release_only'

$dst = '\\svr1\assembly_dept\shedule\release_archive'

$files = Get-ChildItem -Path $src -Include '*.pdf' | Where{$_.LastWriteTime.Date -eq (Get-Date).AddDays(-1)}

ForEach ($file in $files)
{
    Write $file.Name
}

Open in new window

Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

Steve WilliamsProduct Design EngineerAuthor Commented:
Norie, No I do not.
NorieAnalyst Assistant Commented:
Steve

How about this?
$src = '\\svr1\assembly_dept\schedule\release_only'

$files = Get-ChildItem -Path $src | 
Where-Object {$_.LastWriteTime.Date -eq (Get-Date).Date}

ForEach ($file in $files)
{
    Write $file.Name

}

Open in new window

Note, this works for me when I run it with the local folder as the source.
oBdACommented:
Try it like this; it's in test mode and will only show which files it would move (but will/should create the folder structure).
$src = '\\svr1\assembly_dept\schedule\release_only'
$dst = '\\svr1\assembly_dept\shedule\release_archive'

$yesterday = (Get-Date).AddDays(-1).Date
Get-ChildItem -Path $src -Filter '*.pdf' | Where-Object {$_.LastWriteTime.Date -eq $yesterday} | ForEach-Object {
	$dstDir = $dst + '\' + $_.LastWriteTime.Date.ToString('yyyy') + '\' + $_.LastWriteTime.Date.ToString('MM - MMM') + '\' + $_.LastWriteTime.Date.ToString('MMM. dd')
	If (-not (Test-Path -Path $dstDir)) {
		New-Item -ItemType Directory -Force -Path $dstDir
	}
	Move-Item -Path $_.FullName -Destination  $dstDir -WhatIf
}

Open in new window

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
Steve WilliamsProduct Design EngineerAuthor Commented:
oBda, That did the trick worked like a charm. Then I removed the  -WhatIf, run it again and it moved all the files.

Norie, Thanks for your help. Appreciate your time.
Steve WilliamsProduct Design EngineerAuthor Commented:
Thanks all!
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
Powershell

From novice to tech pro — start learning today.