Functions/Get-PSWho.ps1
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 #> # todo - add Credential [CmdletBinding()] Param( [switch] $AsString ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { if ($PSVersionTable.PSEdition -eq 'desktop' -OR $PSVersionTable.OS -match 'Windows') { #get some basic information about the operating system $cimos = Get-CimInstance -classname 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 { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |