PowerTip: Trace What Loads with a PowerShell Module


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

Comments (2)

  1. Walid Toumi says:

    Hi ED,

    thanks for sharing

    here a function to trace module loaded in session:

    Function Trace-Module([switch]$View) {

      $Modules=compare @(powershell -nop -c 'module') @(module) |

           Where { $_.SideIndicator -eq '=>' }

      if($Modules) {

         if($View) {

           $Modules | foreach {

              get-command -Module $_.InputObject |

                  select commandType,Name,ModuleName |  ogv -title "Trace Modules $(date)"

           }

         } else {

           $true

         }

      } else {

         $false

      }

    }

    PS II> trace-Module

    $false

    PS II> ipmo mod2

    PS II> trace-Module

    $true

    PS II> trace-Module -view

  2. Ed Wilson says:

    @Walid Toumi this is a cool function. Well done, thank you for sharing.

Skip to main content