This code works well in Powershell 5+, but does not work in Powershell 4.0 and 2.0:
$DaysToDelete = 2
$targets = "profile/Default",
"profile/Profile 1",
"profile/Profile 2",
"profile/Profile 3",
"profile/Profile 4"
$special = @("chromium", "64")
$profiles = Get-ChildItem "C:\" -Directory -Force |
Where-Object Name -In $special |
Select-Object -ExpandProperty FullName
$chromeDir = "C:\Users\*\AppData\Local\Google\Chrome\User Data\Default"
$chromeSetDir = "C:\Users\*\Local Settings\Application Data\Google\Chrome\User Data\Default"
$Items = @("*Archived History*",
"*Cache*",
"*Cookies*",
"*History*",
"*Top Sites*",
"*Visited Links*",
"*Web Data*")
$profiles | ForEach-Object {
foreach($target in $targets) {
$profile = Join-Path $_ $target
$items | ForEach-Object {
$item = $_
Get-ChildItem $profile, $chromeDir, $chromeSetDir -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) -and $_.Directory -like $item} | ForEach-Object {
$path = Join-Path $_.DirectoryName $_
Remove-Item $path -force -Verbose -recurse -ErrorAction SilentlyContinue }
}
}
}
I found that a fragment that breaks the execution,
-and $_.Directory -like $item
It works great on PS 5+ (Windows 10), but doesn't find anything like PS 4 (Windows 7). The Chrome version and its directory hierarchy are the same on both machines: 59.0.3071.115 (Official Build) (64-bit).
Running script on Win10 with spec support
powershell.exe -Version 4.0
Gave nothing, everything is fine. I'm not so fluent with the Powershell version, so gurus are welcome to offer any suggestions. How to make a script version independent ?
UPDATE: Here is the complete code, but it does not give anything. I checked all the places and precisely localized this problem line above.
: , like , like $_.CreationTime :
$_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete) -and $_.Directory -like $item
, , , , , . p >
.