PowerShell - Automation PatternsWhich of the following is the correct syntax to get files older than 7 days in PowerShell?AGet-ChildItem | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(7) }BGet-ChildItem | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) }CGet-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) }DGet-ChildItem | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(7) }Check Answer
Step-by-Step SolutionSolution:Step 1: Understand date comparison for older filesFiles older than 7 days have LastWriteTime less than current date minus 7 days.Step 2: Match the correct syntaxGet-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } correctly filters files with LastWriteTime less than (Get-Date).AddDays(-7).Final Answer:Get-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } -> Option CQuick Check:Older files filter = LastWriteTime -lt AddDays(-7) [OK]Quick Trick: Use -lt with AddDays(-n) to find files older than n days [OK]Common Mistakes:Using -gt instead of -ltComparing CreationTime incorrectlyAdding days instead of subtracting
Master "Automation Patterns" in PowerShell9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PowerShell Quizzes Cross-Platform PowerShell - PowerShell on macOS - Quiz 13medium Remote Management - SSH-based remoting (PowerShell 7+) - Quiz 12easy Remote Management - CIM/WMI cmdlets - Quiz 2easy Remote Management - CIM/WMI cmdlets - Quiz 12easy Remote Management - PSSession management - Quiz 3easy Remote Management - PowerShell Remoting (Enable-PSRemoting) - Quiz 1easy System Administration - Registry operations - Quiz 12easy System Administration - Why PowerShell automates admin tasks - Quiz 14medium System Administration - Event log reading - Quiz 3easy System Administration - Event log reading - Quiz 5medium