22 April,2021 by Tom Collins
Question: I'm executing a Powershell Script . When I started the Powershell console , it was the "different user" option.
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
This is only a preview. Your comment has not yet been posted.
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.
Posted by: |