PowerTip: Trace What Loads with a PowerShell Module

PowerTip: Trace What Loads with a PowerShell Module

  • Comments 2
  • Likes

Summary: Use the Verbose switched parameter with the Import-Module cmdlet to trace module load activity.

Hey, Scripting Guy! Question How can you watch what loads when importing a Windows PowerShell module?

Hey, Scripting Guy! Answer Use the Verbose switch when calling the Import-Module cmdlet:

Import-Module psdiagnostics -verbose

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Hi ED,</p> <p>thanks for sharing</p> <p>here a function to trace module loaded in session:</p> <p>Function Trace-Module([switch]$View) {</p> <p> &nbsp; $Modules=compare @(powershell -nop -c &#39;module&#39;) @(module) |</p> <p> &nbsp; &nbsp; &nbsp; &nbsp;Where { $_.SideIndicator -eq &#39;=&gt;&#39; }</p> <p> &nbsp; if($Modules) {</p> <p> &nbsp; &nbsp; &nbsp;if($View) {</p> <p> &nbsp; &nbsp; &nbsp; &nbsp;$Modules | foreach {</p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get-command -Module $_.InputObject |</p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select commandType,Name,ModuleName | &nbsp;ogv -title &quot;Trace Modules $(date)&quot;</p> <p> &nbsp; &nbsp; &nbsp; &nbsp;}</p> <p> &nbsp; &nbsp; &nbsp;} else {</p> <p> &nbsp; &nbsp; &nbsp; &nbsp;$true</p> <p> &nbsp; &nbsp; &nbsp;}</p> <p> &nbsp; } else {</p> <p> &nbsp; &nbsp; &nbsp;$false</p> <p> &nbsp; }</p> <p>}</p> <p>PS II&gt; trace-Module</p> <p>$false</p> <p>PS II&gt; ipmo mod2</p> <p>PS II&gt; trace-Module</p> <p>$true</p> <p>PS II&gt; trace-Module -view</p>

  • <p>@Walid Toumi this is a cool function. Well done, thank you for sharing.</p>