1

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
    }
}

2 Answers 2

1

Revised with the original answer to have a proper and cleaner routine to delete/uninstall Teams from old user profiles

$TeamsPath = 'C:\Users\*\AppData\Local\Microsoft\Teams'

$TargetVersion = [version] '1.3.0.13000'

Get-ChildItem -Path $TeamsPath -Filter "Teams.exe" -Force -Recurse | 
  ForEach-Object {
  if ([version] $_.VersionInfo.ProductVersion -le $TargetVersion) {
  
  $UpdateRun = Split-Path $_.DirectoryName 
  
  Start-Process -FilePath "$UpdateRun\Update.exe" -ArgumentList "--uninstall /s" -PassThru -Wait -ErrorAction STOP
  
  #Remove-Item -LiteralPath $_.DirectoryName  -Force -Recurse -WhatIf
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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\*\... not C:\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 the System.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.

1 Comment

Uninstall command for Teams revised answer with the changes that @mklement0 helped on. $UpdateRun = Split-Path $_.DirectoryName ` Start-Process -FilePath "$UpdateRun\Update.exe" -ArgumentList "--uninstall /s" -PassThru -Wait -ErrorAction STOP`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.