I am trying to write a script that will look under all users local appdata, and search for Teams.exe and return the directory where it is below a specific version. The script also (will have to run as system) because it will either be a scheduled task or will be run as a script in SCCM.
I have 2 issues;
Identifying the folder path into a variable that can then pipe the Remove-Item on the end returns error such as cannot find path
It will run perfectly fine when in ISE as local user, but doesn't like it when I run it in Task Scheduler or SCCM Scripts.
I should also mention that I dont want to delete Teams.exe (preferably the Teams folder) from the user path where the quser value returned is not in the c:\Users..... path
One of my scripts is
$current = ((quser) -replace '>' -replace '^\s' -replace '\s{2,}', ',' | ConvertFrom-CSV).Username
$file = "c:\Users\*\AppData\Local\Microsoft\Teams\current\Teams.exe"
$paths = (Get-ItemProperty -Path $file).VersionInfo | Where {$_.ProductVersion -lt "1.6.*" -and $_.FileName}
foreach ($path in $paths) {
Remove-Item $path.FileName -WhatIf | Where $Path.FileName -NotContains $current
Remove-Item $_.Directory.FullName -WhatIf
(Get-ChildItem -Path "C:\Users\*\AppData\Local\Microsoft" -Filter "Teams.exe" -Recurse).DirectoryName | Where {$_.Path -notlike "$current"}
}
Created multiple queries including Split-Path and Join-Path and specifying the where DirectoryName -notcontains quser etc...
Another script is;
$current = ((quser) -replace '>' -replace '^\s' -replace '\s{2,}', ',' | ConvertFrom-CSV).Username
$folder = "C:\Users\$user\Appdata\Local\Microsoft\Teams\"
$file = "Current\Teams.exe"
$fullpath = Join-Path -Path $folder -ChildPath $file
$users = Get-ChildItem -Directory "C:\Users" | Where {$_.Name -notlike $current -and $_.VersionInfo -notlike "1.6.*"}
foreach ($user in $users) {
Remove-Item -Path $folder -Recurse -Force -WhatIf
}
The third attempt looked like this;
$current = ((quser) -replace '>' -replace '^\s' -replace '\s{2,}', ',' | ConvertFrom-CSV).Username
$folder = "C:\Users\$user\Appdata\Local\Microsoft\Teams\"
$file = "Current\Teams.exe"
$fullpath = Join-Path -Path $folder -ChildPath $file
$users = Get-ChildItem -Directory "C:\Users" | Where {$_.Name -notlike $current -and $_.VersionInfo -notlike "1.6.*"}
foreach ($user in $users) {
$folder2 = "C:\Users\$user\Appdata\Local\Microsoft\Teams\"
}
Remove-Item -Path $folder2 -Recurse -Force -WhatIf
This worked for me, a little messy however!
If you don't want to delete the teams folder just remove the following lines: