Powershell System context issue
I tried running the following snippet from SCCM using a package/program deployment.
$ChromeProcesses = Get-Process -Name chrome -ErrorAction SilentlyContinue | Select-Object Path, Id
foreach ($process in $ChromeProcesses) {
if ($process.Path -like "*\AppData\Local\Google\Chrome\Application\chrome.exe") {
Write-Log "Found AppData Chrome - Stopping PID: $($process.id)"
Stop-Process -Id $process.id -Force
}
}
The command line was: powershell.exe -WindowStyle Hidden -File Script.ps1
When testing on a Windows 11 23H2 device, this part of the script would not run. No write-log being performed even though the chrome appdata process was running.
When I changed the command line to: "%Windir%\sysnative\WindowsPowerShell\v1.0\powershell.exe" -WindowStyle Hidden -File Script.ps1 - it worked as expected.
I understand the fix (64 vs. 32-bit Powershell) but I guess my confusion is understanding why a 32-bit powershell can't detect the processes. Appreciate any help understanding this.
Posted May 10, 2024, 12:21 PM UTC
Get-Processemits objects of type[Diagnostics.Process].See the remarks section of the documentation.
Pathis an extended type systemScriptPropertyadded by PowerShell that retrieves its value with$this.Mainmodule.FileName. Per the remark above, asMainModuleis empty for each[Diagnostics.Process]instance, so to is the value ofPath.As you've found, ensuring 64-bit
powershell.exeis launched by bypassing file system redirection withSysnativeis one approach (and most likely, optimal). Another is to use theWin32_Processclass instead.Awesome explanation. Thank you!
You're very welcome.
There are probably a myriad reasons to just use a 64-bit process by default for all tasks, except when 32-bit is required.