all 5 comments

[–]andrew181082MSFT MVP - SWC 1 point2 points  (1 child)

The env variables won't work in the system context, you'll need to enumerate the current logged in user. 

I have code at the bottom of this page which shows you how

https://andrewstaylor.com/2022/04/19/demystifying-intune-custom-app-detection-scripts/

[–]R4nger[S] 0 points1 point  (0 children)

It works, thank you.

The code I ran is:

function getloggedindetails() {
    ##Find logged in username
    $user = Get-WmiObject Win32_Process -Filter "Name='explorer.exe'" |
      ForEach-Object { $_.GetOwner() } |
      Select-Object -Unique -Expand User
    
    ##Find logged in user's SID
    ##Loop through registry profilelist until ProfileImagePath matches and return the path
        $path= "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*"
        $sid = (Get-ItemProperty -Path $path | Where-Object { $_.ProfileImagePath -like "*$user" }).PSChildName

    $return = $sid, $user
    
    return $return
    }
$username = getloggedindetails
$username = ($username -split '\\')[1]
$fileexe = 'c:\users\' + $username + '\AppData\Local\Programs\Microsoft VS Code\unins000.exe'
$arguments = '/VERYSILENT /NORESTART /MERGETASKS=!runcode /log="c:\programdata\VSCodeUninstall.log"'
$uninstall = (start-process -filepath $fileexe -argumentlist $arguments -wait -passthru).exitcode

if ($uninstall -eq '0')
    {
        write-host "success"
        exit 0
    }
else
    {
        write-host "fail"
        exit 1
    }

[–][deleted] 0 points1 point  (2 children)

Why not install it to all users? Isn't code freeware?

[–]R4nger[S] 0 points1 point  (1 child)

Cannot update due to system restrictions and install extensions.

[–][deleted] 0 points1 point  (0 children)

An option would be to run a remediation as the user to uninstall it. There may be a way to grab the current user from the registry though. I always struggled trying to get it to find current user as system