Common Tasks

Get current user name

The Windows PowerShell environment provider lets you access Windows environment variable too. To display the value of an environment variable we can use the following syntax:

     $env:<variable-name>

In this syntax, the dollar sign ($) indicates a variable and the drive name, "env:" indicates an environment variable.

These three variables will give us an info about current user, domain and computer:

$env:username

$env:userdomain

$env:computername

There is also another interesting .NET class that will give us "domain\user"  value:

[Security.Principal.WindowsIdentity]::GetCurrent().Name

<< 1 | 2