This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
as anwer to my question in last post,
how many ways can you find to get the current user from MSH,
I came up with this list : *Edit* - repost WITH pipelines
# environment variable
$env:username
gc env:username
ls env:\username
gci env: -inc username
# registry
(gp "hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\").'Logon User Name'
gp "hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\" | fl 'Logon User Name'
gi "hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\" | gp | select 'Logon User Name'
# .NET
[environment]::username
[System.Security.Principal.WindowsIdentity]::GetCurrent().name
([system.threading.thread]::getdomain()).SetPrincipalPolicy([System.Security.Principal.PrincipalPolicy]::WindowsPrincipal)
[system.threading.thread]::currentPrincipal.Identity.name
([system.threading.thread]::currentPrincipal.identity.get_user()).Translate([system.security.principal.ntaccount])
# COM
(new -com WScript.NetWork).username
(new -com WScript.Shell).Environment('process').item('username')
(new -com WScript.Shell).ExpandEnvironmentStrings("%username%")
# WMI
get-wmiobject Win32_ComputerSystem | fl UserName
(get-wmiobject Win32_ComputerSystem).UserName
feel free to add more :-)
gr /\/\o\/\/
Tags : Monad msh