Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Mac, Linux and Windows

published: March 14, 2024 author: Tinu tags: PowerShell - News categories: PowerShell-Cross-Platform


The magic differents of Mac, Linux and Windows if you working with PowerShell.

Table of Contents

OS Information

I didn’t find any commands for Cross-Platform usage to get the OS-Information.

Windows

On Windows you can use the CIM-Class Win32_OperatingSystem to get some information of the current installed os:

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty Caption
Microsoft Windows Server 2019 Datacenter

Linux

On Linux you can use the /etc/os-release file to get some information of the current installed os:

$OsString = cat /etc/os-release | grep PRETTY_NAME
[regex]::Match($OsString, '\w+\s\d+\.\d+').value
Red Hat Enterprise Linux 9.1

User- and Computername

You can use the command whoami to get the current username or the command hostname to get the current Computername, but there are other ways to get this information.

Mac and Linux

whoami
hostname

Windows

The same as on Mac and Linux, but in PowerShell we use the following commands:

$env:USERNAME
$env:COMPUTERNAME

Cross-Platform

The better way as using whoami and hostname and the Windows-only-commands is to use the DotNet-Method:

[Environment]::UserName
[Environment]::MachineName

Environment variables

This commands lists all the environment variables in the current session:

Cross-Platform

Drive Provider:

Get-Item Env:

.Net Method:

[Environment]::GetEnvironmentVariables()

IsAdmin

Test is the current user is administrator or on Mac and Linux is root:

Mac and Linux

$currentUser = (id -u)
$IsAdmin     = ($currentUser -eq 0)

Windows

$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$IsAdmin     = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

Split-Path

Split the path of the PSModulePath environment variable:

Mac and Linux

$env:PSModulePath -split ':'

Windows

$env:PSModulePath -split ';'

Cross-Platform

If you using the [System.IO.Path]::PathSeparator, then you get the PSModulePath splitted on each Operating System.

$env:PSModulePath -split [System.IO.Path]::PathSeparator

On Mac

/Users/xxx/.local/share/powershell/Modules
/usr/local/share/powershell/Modules
/usr/local/microsoft/powershell/7/Modules

On AlmaLinux

/Users/xxx/.local/share/powershell/Modules
/usr/local/share/powershell/Modules
/opt/microsoft/powershell/7/Modules

Hosts file

Read from the hosts-file:

Windows

$Path = "$($env:windir)\system32\drivers\etc\hosts"
Get-Content $Path

Mac and Linux

$Path = "/etc/hosts"
Get-Content $Path

Temp path

Get the Temp-path:

Windows

$env:tmp

Cross-Platform

[System.IO.Path]::GetTempPath()

← Previous Post [ Top ] Copyright © 2024 by tinuwalther [ Blog ] Next Post →