How to get the current operating system in MSBuild?

Using $(OS), as documented in other answers, can distinguish between Windows_NT and Unix (which includes both Linux and macOS), but not between different Unix-like systems. If you are using MSBuild 15.3 or later (which is very likely), you may want to consider using [MSBuild]::IsOsPlatform():

<Exec Command="./foo.sh" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />

The argument can be the name of any OsPlatform member.


The variable is $(OS), usually checked against being or not being Windows_NT:

<Exec Command="./foo.sh" Condition=" '$(OS)' != 'Windows_NT' " />