Saturday, December 29, 2012

List All Win32/Native Functions Declared/Used By PowerShell

The PowerShell v3 command below will list every P/Invoke declaration made by the assemblies loaded in your PowerShell session. This knowledge may be useful for those seeking to avoid performing their own P/Invoke declarations in PowerShell scripts. What does this mean for those unfamiliar with P/Invoke? This means that you can call the Win32/Native functions listed without needing to compile code (via Add-Type) or getting fancy with reflection.
What you may notice in the resultant output is Microsoft's inconsistency in declaring their DllImport attributes. For example, some dll names are all upper case, some are all lower case, some are camel case, and others lack the dll file extension. Lastly, it's worth noting that the same techniques used to dump the information from all loaded assemblies can be used on all assemblies in the GAC.

Here's the output of the command above from my PowerShell v3 session: