[Question]
  [
### Challenge
Create a program that returns a truthy value when run on Microsoft Windows (for simplicity we'll stick with Windows 7, 8.1 and 10) and a falsey value when run on any other operating system (OSX, FreeBSD, Linux).
### Rules
* Code that fails to run/compile on a platform doesn't count as a falsey value.
### Winning criteria
I'm labelling this as [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so lowest score wins, but I'm also very interested in seeing creative solutions to this problem.
  
[Answer]
# MATLAB, 4 bytes
```
ispc
```
From the [documentation](https://se.mathworks.com/help/matlab/ref/ispc.html):
> 
> `tf = ispc` returns logical 1 (true) if the version of MATLAB® software is for the Microsoft® Windows® platform. Otherwise, it returns logical 0 (false).
> 
> 
> 
There are also the functions [`ismac`](https://se.mathworks.com/help/matlab/ref/ismac.html) and [`isunix`](https://se.mathworks.com/help/matlab/ref/isunix.html). ~~I'll leave it to the reader to figure out what those functions do.~~ [Mego](https://codegolf.stackexchange.com/users/45941/mego) kindly asked for diagrams explaining `ismac` and `isunix` so I've tried to illustrate it here:
[![enter image description here](https://i.stack.imgur.com/o9q7r.jpg)](https://i.stack.imgur.com/o9q7r.jpg)
It was not asked for a diagram of `ispc` but I can reveal that the behaviour is pretty similar, except substitute `OSX` and `Unix` with `Windows`. 
---
**Second approach:**
Here's a second approach with [`getenv`](https://www.mathworks.com/help/matlab/ref/getenv.html) using 23 bytes that should be bullet proof, unless there's another operating system starting with `W`:
```
x=getenv('OS');x(1)==87
```
> 
> `getenv 'name'` searches the *underlying operating system environment
>  list* for text of the form `name=value`, where `name` is the input
>  character vector. If found, MATLAB® returns the character vector
>  value. If the specified name cannot be found, an empty matrix is
>  returned.
> 
> 
> 
[Answer]
# Vim, 2 bytes
```
<C-a>1
```
On Windows, `<C-a>` (ctrl+a) is mapped by default to `Select All`. If you type a `1` in select mode in Windows, it replaces the selection with what you typed (1) leaving a 1 in the buffer.
On other operating systems, `<C-a>` by default is mapped to `Increment number`. Because there's no number to increment, it's a no-op, and then the 1 increases the count but in terms of the buffer is a no-op.
1 is truthy in Vim, and an empty string is falsy
[Answer]
# [Python 2.7.10](https://docs.python.org/2/), 24 bytes
```
import os
0/('['>os.sep)
```
Thanks to FlipTack for 3 bytes
This program takes advantage of the fact that Windows is the only OS to use `\` as a path separator. Normally this is frustrating and bad, but for once it is actually an advantage. On Windows, `'['>os.sep` is false, and thus `0/0` is computed, causing a `ZeroDivisionError` and exiting with a non-zero exit code. On non-Windows platforms, `'['>os.sep` is true, making the expression `0/1`, which does nothing, and the program exits with exit code 0.
[Answer]
## ~~x86 Assembly, 7 bytes~~ (Inspired by [Runemoro's answer](https://codegolf.stackexchange.com/a/106180/63767))
```
31 DB 89 D8 40 CD 80
```
Or
```
xor ebx, ebx 
mov eax, ebx
inc eax
int 0x80
```
## Description
First of all, we'll set `eax` to `1` (the system call number for `exit(int val)` for `Linux`, `FreeBSD` and `OSX`).
Then, we'll call the interrupt gate `0x80` which is the system call gate for `Linux`, `FreeBSD` and `OSX`. That would cause the program to exit with status of `ebx` which is `0` (`false`).
On Windows `int 0x80` is an invalid gate (It uses `2e` as a syscall gate) and would crash the program, causing it to end with a positive exit code (`true`).
**Edit:** Would not work on `OSX` since it has a different argument-passing convention on 32 bit (by the stack).
## References and further reading
* [`FreeBSD - System Calls`](https://www.freebsd.org/doc/en/books/developers-handbook/x86-system-calls.html): Information about system calls in `FreeBSD`.
* [`System calls in the Linux kernel. Part 2.`](https://0xax.gitbooks.io/linux-insides/content/SysCall/syscall-2.html): A great article about Linux's system calls.
* [`Linux Syscall Reference`](http://syscalls.kernelgrok.com/): a system call numbers reference for `Linux`.
* [`Windows Syscall Shellcode`](https://www.symantec.com/connect/articles/windows-syscall-shellcode) : An article about directly calling system calls in `windows` from assembly.
* [`Making system calls from Assembly in Mac OS X`](https://filippo.io/making-system-calls-from-assembly-in-mac-os-x/): An article about system calls on `OSX`.
[Answer]
# PHP, 22 bytes
```
`<?=PATH_SEPARATOR>":";`  
```
prints `1` if the path separator is semicolon (colon or empty for all other OSs except for DOS and OS/2), else nothing.
**also 22 bytes**, but not that safe:
```
<?=strpos(__FILE__,92);
```
prints a positive integer if the file path contains a backslash; else nothing.  
A safe alternative with **27 bytes**: `<?=DIRECTORY_SEPARATOR>"/";` prints `1` or nothing.
**A strange find**: `<?=__FILE__[1]==":";` (**20 bytes**) should be, not safe either, but ok.
But although `__FILE__` pretends to be a string (I tried `var_dump` and `gettype`), indexing it throws an error, unless you copy it somewhere else (concatenation also works) or use it as a function parameter.
Edit:  
`<?=(__FILE__)[1]==":";` (also **22 bytes**) works in PHP 7; but that´s because the parentheses copy the constant´s value to a temporary variable.
**27 bytes**: `<?=stripos(PHP_OS,win)===0;`  
tests if predefined `PHP_OS` constant starts with `win` (case insensitive; Windows,WIN32,WINNT, but not CYGWIN or Darwin); prints `1` for Windows, else nothing.
**17/18 bytes**:
```
<?=strlen("
")-1;
```
prints `1` if it was stored with Windows linebreak (also on DOS, OS/2 and Atari TOS - although I doubt that anyone ever compiled PHP for TOS), else `0`.
You could also check the constant `PHP_EOL`.
**more options:**
`PHP_SHLIB_SUFFIX` is `dll` on Windows, but not necessarily only there.  
`php_uname()` returns info on the operating system and more; starts with `Windows` for Windows.  
`$_SERVER['HTTP_USER_AGENT']` will contain `Windows` when called in a browser on Windows.  
`<?=defined(PHP_WINDOWS_VERSION_BUILD);` (38 bytes) works in PHP>=5.3
**conclusion**
The only failsafe way to tell if it´s really Windows, not anything looking like it, seems to be a check on the OS name. For PHP: `php_os()` may be disabled for security reasons; but `PHP_OS` will probably always contain the desired info.
[Answer]
# C, ~~44~~ ~~43~~ ~~38~~ 36 bytes
*Thanks to @Downgoat for a byte! crossed out 44 is still regular 44*  
*Thanks to @Neil for two bytes!*
```
f(){return
#ifdef WIN32
!
#endif
0;}
```
[Answer]
# Befunge-98, 7 bytes
```
6y2%!.@
```
[Try it online!](http://befunge-98.tryitonline.net/#code=NnkyJSEuQA&input=)
This works by querying the system path separator, which is `\` on Windows and `/` on other operating systems.
```
6y            System information query: #6 returns the path separator.
  2%          Test the low bit - this will be 1 for '/' and 0 for '\'.
    !         Not the value, so it becomes 0 for '/' and 1 for '\'.   
     .@       Output the result and exit.
```
[Answer]
# Mathematica, 28 bytes
```
$OperatingSystem=="Windows"&
```
[Answer]
# Java 8, 33 bytes
*Special thanks to Olivier Grégoire for suggesting `separatorChar`, and Kritixi Lithos for -1 byte!*
This is a lambda expression which returns a boolean. This can be assigned to `Supplier<Boolean> f = ...;` and called with `f.get()`.
```
()->java.io.File.separatorChar>90
```
[**Try it online!**](https://tio.run/nexus/java-openjdk#NY2xCsIwFEXn5CvemAwGV6l2UHBz6igOz5rWJ2kSkpeCSL@9lqLThXsP59IQQ2J44YimMDnTFd8yBW@aEqMjmyoZy91RC63DnOGC5OEjAX5tZuQlxkAPGJZNNZzI99cbYOqzXlGAv2x/DMFZ9DV0cJiV3tTrMwVzJmdNthETckinJ6Z6t52FqJp3ZjuYUNjExczOq870lpXWlRRCiklOcv4C) - the server isn't windows, so this prints `false`. However, in my windows machine, the same code prints `true`.
What this code does is get the System's file seperator, and check whether its codepoint is larger than the character `[`. This true for Windows, as it uses `\` as the seperator - but every other OS uses `/`, which has a lower code in the ASCII table.
[Answer]
# R, 15 bytes
```
.Platform$O>"v"
```
Thanks to [plannapus](https://codegolf.stackexchange.com/users/6741/plannapus) for the suggestion to use partial matching for list element extraction.
[.Platform](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Platform.html) is a list with some details of the platform under which R was built. There is an element `OS.type` (the only element with name starting with "O") which is character string, giving the Operating System (family) of the computer. One of `"unix"` or `"windows"`.
So `"unix"` is less then `"v"`, but `"windows"` is greater then `"v"`. Other valid 15 bytes answers are
```
.Platform$O>"V"
.Platform$O>"w"
.Platform$O>"W"
```
[R is being developed for the Unix-like, Windows and Mac families of operating systems](https://cloud.r-project.org/doc/FAQ/R-FAQ.html#What-machines-does-R-run-on_003f). Other OS families are not supported.
[Answer]
# J, 7 bytes
```
6=9!:12
```
This is a verb (similar to a function) that uses the builtin foreign conjunction `9!:12` to acquire the system type where 5 is Unix and 6 is Windows32.
[Answer]
## x86 machine code, 9 bytes
```
40 39 04 24 75 02 CD 80 C3
```
Compiled from:
```
inc eax        ; set eax to 1
cmp [esp], eax ; check if [esp] == 1 (linux)
jne windows    ; jump over "int 0x80" if on windows
int 0x80       ; exit with exit code 0 (ebx)
windows:
ret            ; exit with exit code 1 (eax)
```
[Answer]
## Perl, 11 bytes
```
print$^O=~MS
```
`^O` should be replaced by a literal Control-O.
Outputs `1` on windows, nothing on another OS.
Note that I'm not using `say` as it adds a trailing newline, which is truthy in Perl.
*-2 bytes thanks to primo. (and fixed potential issues)*  
*-1 bytes thanks to ais523.*
[Answer]
# julia, 10 bytes
```
is_windows
```
A function that returns true for windows
[Answer]
# C#, ~~61~~ 48 bytes
```
()=>(int)System.Environment.OSVersion.Platform<4
```
Saved 13 bytes thanks to TheLethalCoder
Or a full program at 83 bytes:
```
class P{static int Main(){return(int)System.Environment.OSVersion.Platform<4?1:0;}}
```
Various Windows variants use enum values 0 to 3 in the [Microsoft .NET implementation](https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/platformid.cs). 4 is Unix, 5 is Xbox [360] (which I won't consider "Windows"), 6 is MacOSX. [Mono uses the same values](http://mono.wikia.com/wiki/Detecting_the_execution_platform), adding 128 for Unix/Linux in earlier versions.
Therefore, anything < 4 is Windows, and everything else is not Windows.
[Answer]
# JavaScript, ~~42~~ ~~30~~ ~~26~~ 25 bytes
```
console.log((
//Begin
_=>navigator.oscpu[0]>'V'
//End
)())
```
Tested with Firefox. (Chrome doesn't have the `oscpu` property.)
Since lowercase letters have a higher character code than uppercase letters, this depends on the first letter of `navigator.oscpu` being uppercase and not being *W*, *X*, *Y* or *Z* on any platform that Firefox supports (other than Windows, of course). According to [this post](https://stackoverflow.com/a/9514476/1419007), that is the case.
### Edits
1. Saved 12 bytes thanks to [Neil](https://codegolf.stackexchange.com/users/17602/neil).
2. Saved another four bytes
3. Saved another byte thanks to [Blender](https://codegolf.stackexchange.com/users/7800/blender).
[Answer]
## Batch, 50 bytes
```
@if %OS%==Windows_NT if not exist Z:\bin\sh echo 1
```
Edit: Fixed to ignore DOS instead of claiming that it's Windows.
The ~~only~~ other way I know of running Batch outside of Windows is to use WINE which by default will map `Z:` to `/`. Therefore if `Z:\bin\sh` exists, chances are that it's `/bin/sh`, so not MS Windows.
I don't know what WINE sets %OS% to, but if it's not `Windows_NT` then I could save 23 bytes.
[Answer]
# QBasic, 31 bytes
```
?INSTR(ENVIRON$("COMSPEC"),"W")
```
Prints non-zero under Windows, 0 under everything else.
`COMSPEC` is an environment variable unique to Microsoft OSs. It points to the command interpreter, typically `command.com` or `cmd.exe`. Under Windows, the command interpreter sits somewhere in the Windows directory; under MS-DOS, it sits in the DOS directory or on the root of the disk, and under any other OS, it doesn't exist.
By checking to see if the value of `COMSPEC` contains a "W", we can tell the difference between Windows and not-Windows.
[Answer]
# Node.js, ~~27~~ ~~16~~ ~~15~~ 13 bytes
Thanks to **@Patrick**, who shaved 12 bytes off my solution using Node's REPL:
```
_=>os.EOL>`
`
```
Original solution:
```
_=>require('path').sep!='/'
```
[Answer]
# Excel VBA, ~~41~~ ~~40~~ ~~30~~ ~~29~~ ~~26~~ 24 Bytes
Immediate windows function that returns `true` if the system's OS code starts is longer than length 3, because the info is restricted to output either else `mac` or `pcdos` this returns `true` only on windows pcs
```
?[Len(Info("SYSTEM"))>3]
```
**Previous Versions**
```
''# Ignore the second `"` that follows every `\` - its only there for highlighting 
?Left(Environ("OS"),1)="W"                 # 24 Bytes
?InStr(ThisWorkbook.Path,"\"")             # 29 Bytes
?Mid(ThisWorkbook.Path,3,1)="\""           # 30 Bytes, Restricted to local Files
?Application.PathSeparator="\""            # 30 Bytes
?Left(Application.OperatingSystem,1)="W"   # 40 Bytes
```
**Changes**
-1 Thanks to Neil for using `Left(...,1)` over `Mid(...,1,1)`
-10 Thanks to ChrisH for pointing out [@Mego's Path Separator Trick](https://codegolf.stackexchange.com/a/106084/44821 "@Mego's Path Separator Trick") 
-1 For Checking the `WorkbookPath` for `"\"` rather than using `Application.Path Separator`
-4 For switching to `Environ()`
-2 For switching to `[Len(Info(...`
**Novel Solution, 51 bytes**
Novel `sub`routine that outputs, to the VBE immediates window, a `1 (truthy)` under windows and `0 (falsey)` under mac by method of conditional compilation.
```
Sub a
i=1
#If Mac Then
i=0
#End If
Debug.?i
End Sub
```
[Answer]
# [Perl 6](https://perl6.org), ~~19~~ 18 bytes
```
put $*DISTRO.is-win
```
```
put ?($*CWD~~/\\/)
```
Both output `True␤` or `False␤` depending on the system it is run on.
[Answer]
# bash + coreutils, 5 bytes
```
rm $0
```
Also works in most other POSIXy shells. (Note that Windows ports of `bash` and `rm` exist; even though they're only normally used with more heavily POSIXy operating systems, this isn't an entirely vacuous entry.) Outputs via exit code (0 = false, 1 = true). Can be counted as 4 bytes if you're allowed to assume a filename (e.g. `rm a`). Note that this can potentially fail in the case of very weird filenames (which `rm` will interpret as arguments due to the lack of quoting, and possibly delete files you care about, so I'd advise against running this program from a file with a weird name).
Note: deletes the program from disk as a side effect, or at least tries to. In the case where we're running on Windows, the OS will fail to delete the running file (an operation that Windows disallows either by default or full stop), and thus `rm` will error out. `bash` catches the error and converts it into an exit code (thus the program as a whole terminates normally). Most of the other entries here are using 0 for falsey and 1 for truthy in exit codes, so this does the same; note that `bash`'s `if` statement doesn't accept integers at all (rather, it accepts *commands* and branches based on whether they run successfully, and arithmetic tests are done via the means of programs like `test` that intentionally report a "crash" on a failed comparison), so this is on shakier ground in terms of legality than programs that output via exit code in languages where 0 is valid in an `if` statement test and sends the program to the `else` branch.
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 21 [bytes](http://meta.codegolf.stackexchange.com/a/9429/43319)
```
'W'∊∊#⎕WG'APLVersion'
```
[Try it online!](https://tio.run/nexus/apl-dyalog#@///v3q4@qOOLiBSftQ3Ndxd3THAJyy1qDgzP08dAA "APL (Dyalog Unicode) – TIO Nexus")
`#⎕WG'APLVersion'` Root (**#**) **W**indow **G**et property **APL Version**
`∊` enlist (flatten)
`'W'∊` is **W** a member? (no non-Windows return values contain a capital W)
[Answer]
# [TrumpScript](https://github.com/samshadwell/TrumpScript), 17 bytes
```
America is great.
```
[Try it online!](https://tio.run/##KykqzS0oTi7KLCj5/98xN7UoMzlRIbNYIb0oNbFE7/9/AA "TrumpScript – Try It Online")
---
This program, if run on windows, will print:
```
The big problem this country has is being PC
```
This is considered a truthy value.
---
Empty output and the following value are falsy:
```
Boycott all Apple products  until such time as Apple gives cellphone info to authorities regarding radical Islamic terrorist couple from Cal
```
The empty output will occur on any linux system (for this program), the long apple response obviously occurs on Mac (for any program). On TIO, the backend (I'm assuming) is a Unix operating system, so you can only get the falsy value; on my computer I get the PC message.
---
Not 100% sure if this counts as an error message (which would invalidate the answer), but if you didn't know about this it was probably worth a laugh for you.
[Answer]
# tcl, 38 bytes
```
 expr [lsearch $tcl_platform windows]>0
```
[Answer]
# PHP 17 Bytes
The following will output 1 if windows and nothing if anything else. Ignoring notices of string convertion.
`<?=PHP_OS==WINNT;`
[Try online](https://3v4l.org/ra15J) Online tests for linux because the sandbox is linux for PoC.
[Answer]
# Java 8, 49 bytes
```
()->System.getenv().get("OS").contains("Windows")
```
Longer than the [other Java answer](https://codegolf.stackexchange.com/a/106092), but takes a different approach.
This lambda fits in a `Supplier<Boolean>` and can be tested with the following program:
```
public class DetectMSWindows {
  public static void main(String[] args) {
    System.out.println(f(() -> System.getenv().get("OS").contains("Windows")));
  }
  private static boolean f(java.util.function.Supplier<Boolean> s) {
    return s.get();
  }
}
```
[Answer]
# Haskell, ~~39~~ 31 bytes
```
import System.Info
f=os!!0=='m'
```
I check for the first letter output of "m", which should be "mingw" for windows. As far as I could tell, there is no other OS which starts with M. The information comes from <https://github.com/ghc/ghc/blob/master/compiler/utils/Platform.hs>
[Answer]
# [8th](http://8th-dev.com/), 11 bytes
```
 
os 1- not .
 
```
Prints `true` on Windows, `false` on Linux and macOS. Other platforms supported by 8th are Android, iOS and Raspberry Pi, but I am not able to test on them.
Ungolfed version (with comments)
```
 
G:os  \ Return a number n indicating the operating system 
      \ 0 for Linux
      \ 1 for Windows 
      \ 2 for macOS
      \ 3 for Android 
      \ 4 for iOS 
      \ 5 for Raspberry Pi
n:1-  \ Subtract 1
G:not \ If Windows --> true, otherwise --> false
.     \ Print result
 
```
[Answer]
# Python 3 (13 bytes)
```
import winreg
```
Returns with exit code zero (generally true in shells) if on windows, and with a non-zero exit code otherwise.
If you prefer it the other way round, there is a 12 bytes solution: `import posix`.
]