How to identify the the current user on Powershell

22 April,2021 by Jack Vamvas

Question: I'm executing a Powershell Script . When I started the Powershell console , it was the "different user" option.

 

Powershell_user

 

 

Within the powershell script - I want to check\confirm the current user executing the script.

 

Answer:  To identify the current user context utilising the powershell - you can use $env:username

Note: $env:USERNAME is predefined to reflect the current user's username, but is a read-write environment variable. This means it is possible to alter the value - so although it is predefined with the current user , it an be changed

$env:username = 'another_user'      would then display 'another_user'

 

To see a full list of environment variables use Get-Item -Path Env:

Learn  more on Powershell - Expand your Powershell mind – Three key cmdlets

 

 

 

 


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to identify the the current user on Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer