How to Get the Current Username in PowerShell on MacBook Pro

For the last few days, I have been using a MacBook Pro laptop. There, I wanted to get the current username using PowerShell. There are different methods to do so. In this tutorial, I will explain how to get the current username in PowerShell on a MacBook Pro.

Now, let me show you how to retrieve the current username on macOS using PowerShell.

Note: You need PowerShell 7 or later installed on your MacBook Pro.

Method 1: Using Environment Variables

The quickest way to get the current username on a MacBook Pro with PowerShell is through environment variables. On macOS, the USER variable stores the active username.

Environment variables are built into every operating system. On macOS, $env:USER will always return the username of the person running the PowerShell session

$CurrentUsername = $env:USER
Write-Output $CurrentUsername

I executed the above PowerShell cmdlet in my macOS and you can see the exact output in the screenshot below:

Get the Current Username in PowerShell on MacBook Pro

This method is fast and reliable. It’s ideal for scripts that require identifying the user context or personalizing output for different organizations.

Check out How to List Local Users with PowerShell

Method 2: Using .NET’s System.Security.Principal.WindowsIdentity (Cross-Platform Note)

Traditionally, on Windows, you might use the .NET class System.Security.Principal.WindowsIdentity to retrieve the username. However, on macOS, this class is not available. Instead, you can use [System.Environment]::UserName for a cross-platform approach.

The [System.Environment]::UserName property works on both Windows and macOS, making your scripts portable across platforms.

$CurrentUsername = [System.Environment]::UserName
Write-Output $CurrentUsername

I often use this method in cross-platform scripts, as it ensures compatibility and consistency.

Read Delete User Profiles Using PowerShell in Windows 11

Method 3: Using Unix Commands within PowerShell

PowerShell on macOS can execute native Unix commands. The whoami command is a classic way to get the current username.

By calling whoami directly from PowerShell, you leverage the underlying Unix system to fetch the active user.

$CurrentUsername = whoami
Write-Output $CurrentUsername

You can see the exact output in the screenshot below:

PowerShell get current user macOS

This approach is particularly useful if you’re integrating PowerShell with other shell scripts or legacy Unix tools commonly found in enterprise environments.

Check out Track User Login History on Windows Using PowerShell

Method 4: Listing All Logged-In Users

Sometimes, you need to see all users logged into the system, not just the one running the script. This can be helpful for IT audits or multi-user MacBook Pro setups.

The users command lists all users currently logged in. You can capture and process this in PowerShell.

$LoggedInUsers = users
Write-Output $LoggedInUsers

This gives you a snapshot of active user sessions, which is valuable for security and compliance in various organizations.

Here is the output in the screenshot below:

PowerShell username script macOS

Check out Show Logged-In Users with PowerShell

Here is a summary:

MethodCommand ExampleCross-PlatformRecommended For
Environment Variable$env:USERYesSimplicity, speed
.NET Environment Property[System.Environment]::UserNameYesPortability
Unix CommandwhoamiYesUnix integration
List All Logged-In UsersusersYesAudits, multi-user systems

In this tutorial, I explain how to retrieve the current username on a MacBook Pro using PowerShell. We saw several methods with examples.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.