This is my submission for Event 1 in the beginner track for the PowerShell Scripting Games, 2013
Here’s the instructions. The premise of the event is log files have been allowed to accumulate and are now eating disk space on a server. Files accumulate in C:\Application\Log\[Application Name]. We need to identify and move files that are older than 90 days to an archive located at \\NASServer\Archive. When moving, the files should retrain the path with the application name in it. This means C:\Application\Log\App1\OldLogFile.log should land as \\NASServer\Archive\App1\OldLogFile.log. As concise as possible, One-Liners are appreciated.
Here’s my submission:
#Broken Up Get-ChildItem C:\Application\Log\*\* | ForEach { if (((Get-Date)-$_.lastaccesstime).TotalDays -gt 90) { Move-Item $_ \\NASServer\Archive\$(($_.directory).name)\$($_.name) } } #Retained on one line Get-ChildItem C:\Application\Log\*\* | ForEach { if (((Get-Date)-$_.lastaccesstime).TotalDays -gt 90) { Move-Item $_ \\NASServer\Archive\$(($_.directory).name)\$($_.name) } } |