Title: How do I get the current Cmdlet from another object?
Tags: powershell
Question: What's the best way to get the current PowerShell Cmdlet from another object?  If I create a helper object that is not a Cmdlet but will be called by Cmdlets, the helper methods may want to call WriteVerbose, WriteDebug etc.  What's the best way to get access to that?  Is there a static PowerShell method that will return the current Cmdlet or do I need to have the Cmdlet pass itself to the helper?


Here is the accepted answer: AFAICT you will need to pass your cmdlet object to the helper class so it can access those instance methods WriteVerbose, WriteDebug, etc,  I'm not aware of any other public "static" access mechanism to get to these output streams.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:05/22/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<powershell>', 'Title': 'How do I get the current Cmdlet from another object?', 'ViewCount': 108, 'id': 2889701, 'user_AboutMe': '', 'user_AccountId': 32308.0, 'user_DisplayName': 'John Vottero', 'user_Reputation': 805.0, 'user_Views': 97.0, 'user_id': 89516.0}}