Finding the Current User

Should you try and use PowerShell as a log-on script, you may want to know who is actually running the script to access user specific folders or settings. The .NET Environment class can tell you who is running a script.

[Environment]::UserDomainName + "\" + [Environment]::UserName
[Environment]::MachineName

Posted Nov 24 2008, 08:00 AM by ps1

Comments

Aleksandar wrote re: Finding the Current User
on 12-15-2008 12:46 PM

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

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.