Skip to main content

Have a worklet that find and removes appdata for a legacy application from all user profiles on a system. Unfortunately it doesn’t appear to be working when I run it for all endpoints ( worked fine for a single endpoint test ). 

$NameShort = "Q-Pulse"
if ($NameShort.Length -gt 7) {
$NameShort = $NameShort.Substring(0, 4) + "\."
}

$rex = $NameShort + "\..+"
$Paths = @(
"$env:Localappdata\Apps\2.0"
"$env:Localappdata\Deployment"
"$env:Localappdata\Microsoft\Windows\INetCache\IE"
"$env:Temp\Deployment"
)
foreach ($Path in $Paths) {
Get-ChildItem -Recurse $Path | ForEach-Object {
If ($_ -match $rex) {
Remove-Item -Force -Recurse $_.FullName
}
}
}


$startMenuPath = Environment]::GetFolderPath("StartMenu")
$appShortcut = Get-ChildItem -Path $startMenuPath -Recurse -Filter "*$($NameShort)*.appref-ms"

if ($appShortcut) {
Remove-Item -Path $appShortcut.FullName -Force

}

Remove-Item -Path "$env:USERPROFILE\Desktop\$($NameShort).appref-ms" -Force

Any help is greatly appreciated.

Hey abeidson,

Thanks for posting here!

I’m assuming when you mention this runs successfully on a single endpoint it means you’re running this locally correct? If so, I’m imaging the Worklet is having issues leveraging the “$env” variable in your script. When you run a script locally, it will run as the current logged-in user. However, Worklets run as SYSTEM instead and does not interact with the logged-in user.

 

$env:localppdata points to this path as SYSTEM:

C:\WINDOWS\system32\config\systemprofile\AppData\Local

while running it locally points here:

C:\Users\LOGGED_IN_USER\AppData\Local

 

I believe you’ll have to replace the $env variable with the logged in users path. You should be able to accomplish this by first getting the logged in user:

$currentUserName = (Get-CimInstance -Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName) -replace '.*\\', ''

Then replace the $env as such:

C:\Users\$currentUserName\AppData\Local

This this path and see if it works for you!

 

Side note: If you need to test Worklets locally but run them as SYSTEM, you can download PSTools to run scripts as closely as to how they’re ran in Automox.

 

Please let me know if you have any questions!


Howdy Abeidson,

On top of Kyles suggestions, you may be able to get away with copying this and removing-items instead of renaming-items:
 

 


$env:localppdata points to this path as SYSTEM:

C:\WINDOWS\system32\config\systemprofile\AppData\Local

while running it locally points here:

C:\Users\LOGGED_IN_USER\AppData\Local

 

I believe you’ll have to replace the $env variable with the logged in users path. You should be able to accomplish this by first getting the logged in user:

$currentUserName = (Get-CimInstance -Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName) -replace '.*\\', ''

Then replace the $env as such:

C:\Users\$currentUserName\AppData\Local

This this path and see if it works for you!

Thank you for this, it worked like a charm after modifying it based on the above. 

 


Howdy Abeidson,

On top of Kyles suggestions, you may be able to get away with copying this and removing-items instead of renaming-items:
 

 

Thank you for this link. It may come in handy in the future, unfortunately due to the sprawl of the application and the other applications in AppData I couldn’t just rename folders this time around.


Reply