[Environment]::UserName is a cross-platform way to obtain the effective[1] current username, because it uses appropriate platform-native API calls behind the scenes.
Note that on Windows the name returned does not include the domain-name part (a concept that is n/a to Unix-like platforms); use [Environment]::UserDomainName to get the NTLM (non-DNS) form of the domain name on Windows; on Unix-like platforms always and on Windows for non-domain-joined machines, said property returns the (unqualified) machine name.
Note:
Using environment variables is an option too, but is susceptible to tampering / being out of sync with the effective user identity.
If you want to rely on them nonetheless, you can use (assumes PowerShell (Core) 7, which is the only cross-platform PowerShell edition):
$IsWindows ? $env:USERNAME : $env:USER
If a given script must also run in Windows PowerShell (the legacy, ships-with-Windows, Windows-only edition of PowerShell whose latest and last version is 5.1):
($env:USER, $env:USERNAME)[$env:OS -eq 'Windows_NT']
All that said: [Environment]::UserName (a) works in both PowerShell editions and (b) is robust (not susceptible to tampering / being out of sync).
[1] Effective means that the username of the user identity associated with the current thread is reported; while this is the same identity as the one associated with the current logon session by default, it may differ if the current thread is impersonating a different user identity.
If (Linux) then {this way} else {that way}.... 🤷🏼♂️