Powershell 3.5 script converting to 2.0

Can someone help me out here please? I have this powershell script that i use to delete old log files. it works fine on powershell versions 3.5 and higher. Problem is I am now trying to use on a version older and it's a royal pain in the ass trying to figure out the right syntax. Any help would be greatly appreciated. thanks.

-----------------------------script------------------------------------------------------------------
# Delete files older than the $limit.
$limit = (Get-Date).AddDays(-30)
$path = "C:\Program Files\TCI\RetailSuite\HQPM\Logs"

# Delete files older than the $limit.
Get-ChildItem -File $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit} | Remove-Item -Force -ea SilentlyContinue
---------------------------end--------------------------------------------------------------------

--first set of errors i get when i run the above. It also complains on "SilentlyContinue

Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
At C:\Program Files\TCI\RetailSuite\HQPM\HostTran\purgeHqpmLogFiles.ps1:6 char:
20
+ Get-ChildItem -File <<<<  $path -Recurse -Force | Where-Object { !$_.PSIsCont
ainer -and $_.LastWriteTime -lt $limit} | Remove-Item -Force -ea SilentlyContin
ue
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterB
   indingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
   ands.GetChildItemCommand
 
Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
At C:\Program Files\TCI\RetailSuite\HQPM\HostTran\purgeHqpmLogFiles.ps1:6 char:
20
+ Get-ChildItem -File <<<<  $path -Recurse -Force | Where-Object { !$_.PSIsCont
ainer -and $_.LastWriteTime -lt $limit} | Remove-Item -Force -ea SilentlyContin
ue
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterB
   indingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
   ands.GetChildItemCommand
jacobJLAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
# Delete files older than the $limit.
$limit = (Get-Date).AddDays(-30)
$path = "C:\Program Files\TCI\RetailSuite\HQPM\Logs"

# Delete files older than the $limit.
Get-ChildItem $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit} | Remove-Item -Force -ea SilentlyContinue 

Open in new window

As coded, the -File has been superfluous, as the Where-Object checked for "not a directory" anyway, which is the purpose of that switch ...
0
 
jacobJLAuthor Commented:
Hmmm thanks Qlemo, that worked this time except that was the first thing I tried, and then got an error on the "ea SilentlyContinue". So i removed that as well and got an error that one of the files was currently in use. What can i replace Silentlycontinue with so that if a file is in use it will skip over it?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
No clue why it should complain about -ea SilentlyContinue. It is part of the syntax of Remove-Item even with PS 2.
0
 
jacobJLAuthor Commented:
Ah. well i went back and looked at the error message and noticed i spelled it wrong. Thank you very much for your help.
0
 
jacobJLAuthor Commented:
Thank you for the help. Greatly appreciated
0
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.

All Courses

From novice to tech pro — start learning today.