Wrecking my logic circuits as I can't comprehend Powershell with some .NET thrown together for this compare file array results. From the array results, script is then to compare each results file version against a set version and then to delete the file if is below/not equal to the set version. I tried to use sample Comparing file versions in Powershell but it I cannot find the output issue as to why the behavior works on a Windows Server 2019 vs a Windows 10 Enterprise. Deleting old profile Teams.exe data from the MS Teams Machine Wide crap. Your assistance will be most appreciated!
My script deploys through MS Endpoint/SCCM
$TeamsPath = 'C:\Users*\AppData\Local\Microsoft\Teams\current\Teams.exe'
$FileVersion = @(Get-ChildItem -Path $TeamsPath -Recurse -Force)
foreach ($TeamsPath in $FileVersion){
$ProductVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($TeamsPath).ProductVersion
$TargetVersion = [System.Version]::Parse("1.3.0.13000")
if ($ProductVersion -le $TargetVersion){
Remove-Item -Path $TeamsPath.Directory -Force -Recurse
}
}
CodePudding user response:
The problems with your code:
(This may be posting artifact) You're looking to loop over all users'
AppData
folders, so the wildcard path must start with'C:\Users\*\...
notC:\Users*\...
- that is,*
must be its own path component.There's no need to call
[System.Diagnostics.FileVersionInfo]::GetVersionInfo($TeamsPath)
, because the.VersionInfo
ETS property that PowerShell decorates theSystem.IO.FileInfo
instances with performs exactly that call behind the scenes.The
.ProductVersion
property contains a string representation of a version number; if you use it as the LHS of your-le
comparison, string comparison is performed (the[version]
(System.Version
) RHS is then coerced to a string too).
The following is a streamlined version of your code with the problems corrected:
$teamsPath = 'C:\Users\*\AppData\Local\Microsoft\Teams\current\Teams.exe'
$targetVersion = [version] '1.3.0.13000'
Get-ChildItem -Path $teamsPath -Force |
ForEach-Object {
if ([version] $_.VersionInfo.ProductVersion -le $targetVersion) {
Remove-Item -LiteralPath $_.DirectoryName -Force -Recurse -WhatIf
}
}
Note: The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
once you're sure the operation will do what you want.