by VPham at 2012-10-24 14:40:18
Hi Guysby coderaven at 2012-10-24 14:46:26
Im new to PS scripting and currenlty building my knowledge as i go . A few work mates and my self are currenlty working on a little task to delete any files thats older than 7 days using an application called JAMS(Job access managemnet system).
whats required : i need to write a PS script to delete files older than X Daysm in this case any files older than 7days
PS script i have drafted is below but fails with the following message :
$TargetFolder = "<<Path>>"
Get-ChildItem $TargetFolder -recurse -include *.pdf | Where-Object $.CreationTime -lt ($(Get-Date).AddDays(-7)) | Remove-Item $.FullName -force
Fails with the following
------------------------------------
Error: Where-Object : Cannot bind argument to parameter ‘FilterScript’ because it is n
Error: ull.
Error: At C:\ProgramData\JAMS\Temp\xPression_Task_OBD_Print_Powershell_0000CC9A.ps1:8
Error: char:67
Error: + Get-ChildItem $TargetFolder -recurse -include *.pdf | Where-Object <<<< $.C
Error: reationTime -lt ($(Get-Date).AddDays(-7)) | Remove-Item $.FullName -force
Error: + CategoryInfo : InvalidData: ( [Where-Object], ParameterBindin
Error: gValidationException
Error: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
Error: icrosoft.PowerShell.Commands.WhereObjectCommand
Error:
Any help , would be much appreciated.
Thanks All.
Use {} around your Where-Object filter like this:by VPham at 2012-10-24 14:57:26Where-Object {$.CreationTime -lt ($(Get-Date).AddDays(-7))}
Cheers coderaven ,by DonJ at 2012-10-24 15:20:35
have added {} but failed with the same error, have copied the error below.
Get-ChildItem $TargetFolder -recurse -include *.pdf | Where-Object {$.CreationTime -lt ($(Get-Date).AddDays(-7))} | Remove-Item $.FullName -force
Error: Remove-Item : Cannot bind argument to parameter ‘Path’ because it is null.
Error: At C:\ProgramData\JAMS\Temp\xPression_Task_OBD_Print_Powershell_0000CC9C.ps1:9
Error: char:75
Error: + Where-Object {$.CreationTime -lt ($(Get-Date).AddDays(-7))} | Remove-Item <<
Error: << $.FullName -force
Error: + CategoryInfo : InvalidData: ( [Remove-Item], ParameterBinding
Error: ValidationException
Error: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
Error: icrosoft.PowerShell.Commands.RemoveItemCommand
Error:
You can’t use $ outside of the Where filter script. Remove $_.FullName entirely. Let it pick up the path and file name from the pipeline.