Scripting Question

Powershell - detect the removal of applications

08/02/2017 3050 views
Hi All,
I have been learning Powershell and a bit stuck with compiling a count.  My script is
$users = Get-ChildItem -Path "C:\Users"
$users | ForEach-Object {
$counter =Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "file.txt" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
if ($counter -lt "1" ) {Write-Host "files not exist"}

What I'm trying to do is write a custom SCCM script for detecting the presence of (in this case the absence of) a deployment type.
SCCM will detect a screen echo as deployment type present.  My script above looks for the file "file.txt" and outputs an echo every time it doesn't see that file.  There is 4 profiles on this VM and so the output says  ({blank} is for an empty line)
files not exist
files not exist

I need the script to count the screen echo's and output a counter, so then I can say if $totalcounter -lt 1 write-host "something"

I hope that says it all.  Please ask questions if this doesn't help

Answer Summary:
2 Comments   [ + ] Show comments


  • For a PowerShell detection method, shouldn't you just be writing out if the file is detected (not writing out if the file isn't detected)? Also are your installations not per-machine? Just wondering why you're searching through multiple profiles for a file. And finally can you not utilise Test-Path to check for the presence of a file instead?
  • I have been tasked with removing Firefox from all machines. This includes anybody who has installed into appdata.

    light bulb - I think I know what you are asking - I will get breakfast and a coffee and I will investigate. I spent too much time yesterday on a single path, from something we did in VBS a long time ago

Answer Chosen by the Author

I finally had a break through in the times I could afford to devote to this
First I had to run compliance rules to detect Firefox in the user directories, this is the detection for setting up compliance
$users = Get-ChildItem -Path "C:\Users"
$users | ForEach {

$counter =Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "Firefox.exe" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
if ($counter -gt "0" ) {exit}
Write-Host "done"

From what I have read and tested, SCCM has script detection from an echo command, so if you want something to say when it's not installed, you have to detect the absence of the application then echo something.

After that, the uninstall script

#Uninstall Firefox User rights installed
get-childitem "C:\users" firefox.exe -Recurse -Force | foreach-object {
$setup = $_.directory.tostring() + "\uninstall\helper.exe"
$args = " /s"
$uninst = Start-Process $setup -PassThru -ArgumentList $args -wait

#Delete registry key
$null = New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS
Set-Location HKU:
Get-ChildItem -path HKU: | ForEach-Object {$uninstall = $_.Name + "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"

if((Test-Path "hklm:\Software\Mozilla\Firefox") -eq 'true'){
Remove-Item "hklm:\Software\Mozilla\Firefox" -Recurse -Force
if((Test-Path "hklm:\SOFTWARE\Wow6432Node\Mozilla\Firefox") -eq 'true'){
Remove-Item "hklm:\SOFTWARE\Wow6432Node\Mozilla\Firefox" -Recurse -Force

If ($uninstall -contains "*firefox*"){
$firefox = get-ChildItem -path $uninstall | ?{$_.Name -like "*Firefox*"}

Remove-Item -path $firefox -Recurse -ErrorAction SilentlyContinue

Then the detection to make sure the uninstall script ran.  This rule has to be the opposite of the compliance rule.

$users = Get-ChildItem -Path "C:\Users"
$users | ForEach {

$counter =Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "Firefox.exe" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
if (!($counter -eq $false)) {exit}

Write-Host "done"}

I hope this helps people to uninstall their unwanted stuff from their domain.

Please don't forget to rate this if it's helpful

Answered 08/07/2017 by: Tempril
Purple Belt

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:


This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ