Detect if process executes inside a Windows Container Detect if process executes inside a Windows Container powershell powershell

Detect if process executes inside a Windows Container


New readers can skip ahead to the part marked with "Update" which contains the accepted solution.

A quick check with whoami on the command prompt showed that the combination of domain and username that is used inside a container seems to be rather unusual. So I used this code to solve the problem:

function Test-IsInsideContainer {  if( ($env:UserName -eq "ContainerAdministrator") -and ($env:UserDomain -eq "User Manager") ) {    $true  }  else {    $false  }}

Update: Another option is to check if the service cexecsvc exist. An internet search did not yield much information about this service, but its name (Container Execution Agent) suggests that it only exists inside of containers (which I verified with some quick test on Win10 and a Server2016 Docker-host).So maybe this code meets your requirements (I am a newbie in Powershell):

function Test-IsInsideContainer {  $foundService = Get-Service -Name cexecsvc -ErrorAction SilentlyContinue  if( $foundService -eq $null ) {    $false  }  else {    $true  }}


There's a "ContainerType" registry value under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control.

So:

function Test-IsInsideContainer {  $containerType = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control").ContainerType  $containerType -ne $null}


Will below work?PS C:\> (Get-NetAdapter).Name -match "container"True