Change a folder LastWriteTime based on files within it

A few days ago I wrote a script that copies some files. Did notice that everything except the date on the folders were ok. So I added a few more lines of powershell code.

Did find a few suggestions on the web, but I like this one…. Since I wrote it. :-P

Function Set-FolderDate {
        Param (
                [string] $Path
        )
        Trap  [Exception] {
                Write-Debug $("TRAPPED: " + $_.Exception.Message);
                Write-Verbose "Could not change date on folder (Folder open in explorer?)"
                Continue
        }

        # Get latest filedate in folder
        $LatestFile = Get-ChildItem $Path | Sort-Object LastWriteTime -Descending | Select-Object -First 1

        # Change the date, if needed
        $Folder = Get-Item $path
        if ($LatestFile.LastWriteTime -ne $Folder.LastWriteTime) {
                Write-Verbose "Changing date on folder '$($Path)' to '$($LatestFile.LastWriteTime)' taken from '$($LatestFile)'"
                $Folder.LastWriteTime = $LatestFile.LastWriteTime
        }

        Return $Folder
}

Set-FolderDate -Path "D:\temp"

Unzip multiple files via Powershell

Simple and effective.

PARAM (
        [string] $ZipFilesPath = "X:\Somepath\Full\Of\Zipfiles",
        [string] $UnzipPath = "X:\Somepath\to\extract\to"
)

$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($UnzipPath)

$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP

$progress = 1
foreach ($ZipFile in $ZipFiles) {
        Write-Progress -Activity "Unzipping to $($UnzipPath)" -PercentComplete (($progress / ($ZipFiles.Count + 1)) * 100) -CurrentOperation $ZipFile.FullName -Status "File $($Progress) of $($ZipFiles.Count)"
        $ZipFolder = $Shell.NameSpace($ZipFile.fullname)

        $Location.Copyhere($ZipFolder.items(), 1040) # 1040 - No msgboxes to the user - http://msdn.microsoft.com/en-us/library/bb787866%28VS.85%29.aspx
        $progress++
}

Btw… Watch out… there is a -Recurse on gci… :-)