tags:

views:

1474

answers:

3

What i'm trying to do is create a powershell script which gets a list of folders in a directory which is filtered by a regular expression screening out folder names with nnnnnnx31 or nnnnnnddd where n = alpha chars for the first 6 chararcts and the last 3 is either numbers of the static string x31. Next it screens if the files are 90 days old which will be copied to a different directory.

When i try to run : Posh>get-childitem | where {$.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$.lastwritetime -lt (get-date.adddays(-90)}

I get the error: You must provide a value expression on the right-hand side of the -lt operator At line: 1 char: 96 + get-childitem | where {$.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$.lastwritetime -lt <<<< get-date.adddays(-90)}

I also tried the following and it didn't work.. Posh>get-childitem | where {$.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$.lastwritetime -lt (get-date | foreach-object {$_.adddays(-90)}) }

Any ideas?

+3  A: 

you need to do (get-date).AddDays(-90)

Scott Weinstein
thanks.. it was just a matter of adding parenthesis
phill
+1  A: 

try this - get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$_.lastwritetime -lt (get-date).adddays(-90)}

audiphilth
+1  A: 

You can use one where-object instead of two:

get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$" -AND $_.lastwritetime -lt (get-date).adddays(-90)}
Shay Levy
upvote for combining the where clauses. terseness and perf improvment.
James Pogran