Rename pictures in a folder to date taken using PowerShell

To rename all the pictures in a folder to their date taken value, run the following script in a PowerShell in that folder:

$shell = New-Object -ComObject shell.application
$ukCulture = [Globalization.CultureInfo]'en-GB'
Get-ChildItem *.jpg | ForEach{
$folderpath = $_.DirectoryName
$folder = $shell.NameSpace($_.DirectoryName)
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
$datetime = [DateTime]::Parse($RawDate,$ukCulture)
$DateTaken = $datetime.ToString("yyyyMMdd_HHmm")
$seconds = $datetime.ToString("ss")
$newpath = $folderpath + "\" + $DateTaken + $seconds + $_.Extension
If(Test-Path -Path $newpath){
while ((Test-Path -Path $newpath) -eq $true)
    {$secondsValue=[int]$seconds + 1;
     $seconds = ([string]$secondsValue).Padleft(2,'0')
     $newpath = Join-Path $folderpath ($DateTaken + $seconds + $_.Extension)}
     Rename-Item $_.pspath -NewName $newpath}
else {Rename-Item $_.pspath -NewName $newpath} }

Since the date taken doesn't record seconds, if there are multiple pictures taken at the same time, I'm incrementing a counter and using it as the "seconds" value in the file name.