

Returns /usr/bin/dotnet on Linux/MacOS and \usr\bin\dotnet on Windows. Not only does it use the correct directory separator, but it converts directory separators to the directory separator for the current platform.įor example, Join-Path -Path '\usr\bin' -ChildPath 'dotnet' That path won’t work on Linux or MacOS because their file systems see the \ character as an escape character, not a directory separator. Never, ever put paths together with strings, e.g.
#Get file path on mac code
Always Use “Join-Path” to Create Path Strings When the Path Originates in Your Code Since environment variable names are case-insensitive on Windows, you should prefer the case from Linux/MacOS. Would return nothing on MacOS or Linux, because the path environment variable is PATH on those platforms. Use the Same Case When Reading/Setting Environment VariablesĮnvironment variable names are case-sensitive on MacOS and Linux, regardless of how you access them. The Environment class also has neat properties like Is64BitProcess, Is64BitOperatingSystem, and UserInteractive, which aren’t exposed in the env: drive. They return the correct values across operating systems. Instead of using environment variables like $env:USERNAME, use the static properties on the Environment class instead. Windows and MacOS both have variables for the temp directory, but they have different names. All of them have PATH, but not much else. Use “Environment” Class Properties Instead of “env:” DriveĮnvironment variables are different between the different operating systems.

I learned a lot that I wanted to share with the community as cross-platform support becomes more and more important. I just spent a month updating one of our PowerShell modules to support Linux and MacOS.
