Reputation: 33
I'm trying to learn a bit of Powershell and have been converting a long archive script from Dos to Powershell. This has been working out quite well, but now I'm exploring options to rewrite the following part, if the field Recursive from an xml document is true, get-childitem has to use the -Recurse flag. Offcourse it would be nice if I could to this in one sentence, any ideas?:
if ($parameter.Recursive -eq "true")
{ $items = Get-ChildItem $parameter.ProcesDir -Recurse | where {!$_.PsisContainer -and $_.CreationTime -lt (get-date).adddays(-$parameter.Retention)}}
else
{ $items = Get-ChildItem $parameter.ProcesDir | where {!$_.PsisContainer -and $_.CreationTime -lt (get-date).adddays(-$parameter.Retention)}}
Upvotes: 1
Views: 1008
Reputation: 126762
Here's one way:
$recurse = if($parameter.Recursive -eq "true") {$true} else {$false}
$items = Get-ChildItem $parameter.ProcesDir -Recurse:$recurse |
where {!$_.PsisContainer -and $_.CreationTime -lt (get-date).adddays(-$parameter.Retention)}
Upvotes: 2