Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Who is Running Your PowerShell Script?

Posted on November 21, 2017November 21, 2017

I've often talked about the benefit of including Verbose output in your PowerShell scripts and functions from the very beginning. This is especially helpful when someone else is running your command but encounters a problem. You can have them start a transcript, run your command with –Verbose, close the transcript and send it to you. if you've written informative Verbose messaging you should be able to figure out the problem. Part of the information might include metadata about the person running the command and their environment. To simply things, I've created an easy to use function called Get-PSWho.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The function writes a custom object to the pipeline with information about the user, their computer and their PowerShell environment. One of the things that might be helpful is knowing where the command is being run as not all PowerShell hosts may support your code.

image

 

image

image

The other design decision I made was to deviate slightly from the recommended practice of only writing one type of object to the pipeline and not re-inventing the wheel. One of the reasons I wrote this was to make it easy to incorporate into your functions and scripts. You can either embed the function or make sure it is distributed with your commands so that you can include the output in a Write-Verbose line. But, you have to make turn the output into a string.

Write-Verbose "User Metadata"
Write-Verbose (Get-PSWho | Out-String)

Since you might need a string anyway, I decided to include it as an option.

Write-Verbose "User Metadata"
Get-PSWho -AsString | Write-Verbose

image

Or you could just as easily pipe the string version to Write-Host. The point is that I thought about how this command might be used and added what I thought would be a useful parameter. Again, I am a bit torn on whether this is the right design decision, but you may simple opt to take my code and paste it into your function directly.

You can find the code on GitHub.

Because I'm using Get-CimInstance, this command won't run in PowerShell Core on non-Windows systems. Although I can probably take that into account and code around it.

If you encounter problems with it or have suggestions, please post an issue on the Gist page.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Who is Running Your PowerShell Script?”

  1. Jeffery Hicks says:
    November 27, 2017 at 10:00 am

    The current version on GitHub is designed to work with PowerShell Core, although it has not been tested on all platforms.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d