Last active
November 10, 2023 15:58
-
-
Save jdhitsolutions/3ecc6193ab0982d907c2db3f7d2bd15d to your computer and use it in GitHub Desktop.
A PowerShell function for getting user metadata. See help for more details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-PSWho { | |
| <# | |
| .SYNOPSIS | |
| Get PowerShell user summary information | |
| .DESCRIPTION | |
| This command will provide a summary of relevant information for the current | |
| user in a PowerShell session. You might use this to troubleshoot an end-user | |
| problem running a script or command. The default behavior is to write an | |
| object to the pipeline, but you can use the -AsString parameter to force the | |
| command to write a string. This makes it easier to use in your scripts with | |
| Write-Verbose. | |
| .PARAMETER AsString | |
| Write the summary object as a string. | |
| .EXAMPLE | |
| PS C:\> Get-PSWho | |
| User : BOVINE320\Jeff | |
| Elevated : True | |
| Computername : BOVINE320 | |
| OperatingSystem : Microsoft Windows 10 Pro [64-bit] | |
| OSVersion : 10.0.16299 | |
| PSVersion : 5.1.16299.64 | |
| Edition : Desktop | |
| PSHost : ConsoleHost | |
| WSMan : 3.0 | |
| ExecutionPolicy : RemoteSigned | |
| Culture : en-US | |
| .EXAMPLE | |
| PS /mnt/c/scripts> get-pswho | |
| User : jhicks | |
| Elevated : NA | |
| Computername : Bovine320 | |
| OperatingSystem : Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 | |
| OSVersion : Ubuntu 16.04.3 LTS | |
| PSVersion : 6.0.0-rc | |
| Edition : Core | |
| PSHost : ConsoleHost | |
| WSMan : 3.0 | |
| ExecutionPolicy : Unrestricted | |
| Culture : en-US | |
| .EXAMPLE | |
| PS C:\Program Files\PowerShell\6.0.0-rc> get-pswho | |
| User : BOVINE320\Jeff | |
| Elevated : True | |
| Computername : BOVINE320 | |
| OperatingSystem : Microsoft Windows 10 Pro [64-bit] | |
| OSVersion : 10.0.16299 | |
| PSVersion : 6.0.0-rc | |
| Edition : Core | |
| PSHost : ConsoleHost | |
| WSMan : 3.0 | |
| ExecutionPolicy : RemoteSigned | |
| Culture : en-US | |
| .EXAMPLE | |
| PS C:\> Get-PSWho -asString | Set-Content c:\test\who.txt | |
| .NOTES | |
| Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ | |
| .INPUTS | |
| none | |
| .OUTPUTS | |
| [pscustomboject] | |
| [string] | |
| .LINK | |
| Get-CimInstance | |
| .LINK | |
| Get-ExecutionPolicy | |
| .LINK | |
| $PSVersionTable | |
| .LINK | |
| $Host | |
| #> | |
| [CmdletBinding()] | |
| Param( | |
| [switch]$AsString | |
| ) | |
| if ($PSVersionTable.PSEdition -eq "desktop" -OR $PSVersionTable.OS -match "Windows") { | |
| #get some basic information about the operating system | |
| $cimos = Get-CimInstance win32_operatingsystem -Property Caption, Version,OSArchitecture | |
| $os = "$($cimos.Caption) [$($cimos.OSArchitecture)]" | |
| $osver = $cimos.Version | |
| #determine the current user so we can test if the user is running in an elevated session | |
| $current = [Security.Principal.WindowsIdentity]::GetCurrent() | |
| $principal = [Security.Principal.WindowsPrincipal]$current | |
| $Elevated = $principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) | |
| $user = $current.Name | |
| $computer = $env:COMPUTERNAME | |
| } | |
| else { | |
| #non-Windows values | |
| $os = $PSVersionTable.OS | |
| $lsb = lsb_release -d | |
| $osver = ($lsb -split ":")[1].Trim() | |
| $elevated = "NA" | |
| $user = $env:USER | |
| $computer = $env:NAME | |
| } | |
| #object properties will be displayed in the order they are listed here | |
| $who = [pscustomObject]@{ | |
| User = $user | |
| Elevated = $elevated | |
| Computername = $computer | |
| OperatingSystem = $os | |
| OSVersion = $osver | |
| PSVersion = $PSVersionTable.PSVersion.ToString() | |
| Edition = $PSVersionTable.PSEdition | |
| PSHost = $host.Name | |
| WSMan = $PSVersionTable.WSManStackVersion.ToString() | |
| ExecutionPolicy = (Get-ExecutionPolicy) | |
| Culture = $host.CurrentCulture | |
| } | |
| if ($AsString) { | |
| $who | Out-String | |
| } | |
| else { | |
| $who | |
| } | |
| } #end Get-PSWho |
Author
Author
This function was first described at jdhitsolutions.com/blog/powershell/5789/who_is_running
Author
This command is now a part of https://github.com/jdhitsolutions/PSScriptTools.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The latest version has been updated to work with PowerShell Core, although it has not been tested on all platforms.