Reputation: 23
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
Upvotes: 0
Views: 2543
Reputation: 1
This worked for me, a little messy however!
$fixedVersion = '1.6.0.18681'
$TopFolder = 'C:\Users'
$ExePath = '\AppData\Local\Microsoft\Teams\current\Teams.exe'
$DeletionPath = '\AppData\Local\Microsoft\Teams\'
$UserFolderList = Get-ChildItem $TopFolder -Directory -Exclude Default, Public
foreach ($Item in $UserFolderList)
{
if (Test-Path (Join-Path -Path $Item -ChildPath $ExePath)){
$exeLocated = Join-Path -Path $Item -ChildPath $ExePath
if (Get-ChildItem -Path $exeLocated | Where-Object {$_.VersionInfo.FileVersionRaw -lt $fixedVersion})
{
Write-Host "Outdated Version Found at $exeLocated"
$Delete = Join-Path $Item -ChildPath $DeletionPath
Remove-Item -Path $Delete -Recurse -Force
}
}
}
If you don't want to delete the teams folder just remove the following lines:
$Delete = Join-Path $Item -ChildPath $DeletionPath
Remove-Item -Path $Delete -Recurse -Force
Upvotes: 0