Lessons Learned from the Scripting Games Advanced Event 1

This is a continuation from my previous blog titled 2013 PowerShell Scripting Games Advanced Event 1 – Parameters Don’t Always Work As Expected.

This isn't the exact script, but sections of it. You'll notice at the bottom of the first image shown below, I retrieve the list of folder names from the files variable to keep from having to make another call to the file system. Going from one variable to another in memory is a cheap operation where as going to disk to retrieve something is more expensive from a resources standpoint. I received this comment from a reviewer: I thought this was pretty clever: $folders = $files.directory.name | Select-Object -Unique.


Then I iterate through the list of folders one at a time instead of iterating through each file individually. I build my paths, storing them in variables. I check to see if the folder exists and create it if not, and then move the files a folder at a time. My goal was to make as few calls to the file system as possible.


It's not only about writing a command that's syntax is correct, or a command that works, but also a command that's as efficient as possible. I've trimmed out the error checking and verbose output from these examples to make them easier to understand.

Update 02/09/14:

The link to my solution is no longer valid so I’m posting it here since I’ve received some requests for it:

 1function Move-LogFile {
 5Move files with a .log extension from the SourcePath to the DestinationPath that are older than Days.
 7Move-LogFile is a function that moves files with a .log extension from the first level subfolders that are specified via
 8the SourcePath parameter to the same subfolder name in the DestinationPath parameter that are older than the number of days
 9specified in the Days parameter. If a subfolder does not exist in the destination with the same name as the source, it will
10be created. This function requires PowerShell version 3.
11.PARAMETER SourcePath
12The parent path of the subfolders where the log files reside. Log files in the actual SourcePath folder will not be archived,
13only first level subfolders of the specified SourcePath location.
14.PARAMETER DestinationPath
15Parent Path of the Destination folder to archive the log files. The name of the original subfolder where the log files reside
16will be created if it doesn't already exist in the Destination folder. Destination subfolders are only created if one or more
17files need to be archived based on the days parameter. Empty subfolders are not created until needed.
19Log files not written to in more than the number of days specified in this parameter are moved to the destination folder location.
21Switch parameter that when specified overwrites destination files if they already exist.
23Move-LogFile -SourcePath 'C:\Application\Log' -DestinationPath '\\NASServer\Archives' -Days 90
25Move-LogFile -SourcePath 'C:\Application\Log' -DestinationPath '\\NASServer\Archives' -Days 90 -Force
28    [CmdletBinding()]
29    param (
30        [string]$SourcePath = 'C:\Application\Log',
31        [string]$DestinationPath = '\\NASServer\Archives',
32        [int]$Days = 90,
33        [switch]$Force
34    )
36    BEGIN {
37        Write-Verbose "Retrieving a list of files to be archived that are older than $($Days) days"
38        try {
39            $files = Get-ChildItem -Path (Join-Path -Path $SourcePath -ChildPath '*\*.log') -ErrorAction Stop |
40                     Where-Object LastWriteTime -lt (Get-Date).AddDays(-$days)
41        }
42        catch {
43            Write-Warning $_.Exception.Message
44        }
46        $folders = $files.directory.name | Select-Object -Unique
47        Write-Verbose "A total of $($files.Count) files have been found in $($folders.Count) folders that require archival"
48    }
50    PROCESS {
51        foreach ($folder in $folders) {
53            $problem = $false
54            $ArchiveDestination = Join-Path -Path $DestinationPath -ChildPath $folder
55            $ArchiveSource = Join-Path -Path $SourcePath -ChildPath $folder
56            $ArchiveFiles = $files | Where-Object directoryname -eq $ArchiveSource
58            if (-not (Test-Path $ArchiveDestination)) {
59                Write-Verbose "Creating a directory named $($folder) in $($DestinationPath)"
60                try {
61                    New-Item -ItemType directory -Path $ArchiveDestination -ErrorAction Stop | Out-Null
62                }
63                catch {
64                    $problem = $true
65                    Write-Warning $_.Exception.Message
66                }
67            }
69            if (-not $problem) {
70                Write-Verbose "Archiving $($ArchiveFiles.Count) files from $($ArchiveSource) to $($ArchiveDestination)"
71                try {
72                    If ($Force) {
73                        $ArchiveFiles | Move-Item -Destination $ArchiveDestination -Force -ErrorAction Stop
74                    }
75                    Else {
76                        $ArchiveFiles | Move-Item -Destination $ArchiveDestination -ErrorAction Stop
77                    }
78                }
79                catch {
80                    Write-Warning $_.Exception.Message
81                }
82            }
84        }
85    }
87    END {
88        Remove-Variable -Name SourcePath, DestinationPath, Days, Force, files, folders, folder,
89        problem, ArchiveDestination, ArchiveSource, ArchiveFiles -ErrorAction SilentlyContinue
90    }